Hi! I'm getting
Exception in thread "main" java.lang.NullPointerException
and I really don't understand why it's happening.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi! I'm getting
Exception in thread "main" java.lang.NullPointerException
and I really don't understand why it's happening.
Last edited by theandroidgirl; July 19th, 2014 at 01:45 AM.
Please post the entire error message. Furthermore, do you know what a NullPointerException is and what it means if you get one?
Perhaps google the term "NullPointerMessage" and read up on some tutorials about it.
Yes exactly. A NullPointerException happens when you try to call a method on an object, but the object does not exist.
For example, compare these two bits of code:
String s = "Hello World"; String[] arr = s.split(" ");String s = null; String[] arr = s.split(" ");
The first one will work and return an array of "Hello" and "World".
The second piece of code will throw a NullPointerException. The reason is, that we have a variable s and we try to call a method on the object that is being referenced by s.
But the problem is, that there is no object being referenced by s. In our example s is a NullPointer, its a variable referencing the null value.
Somewhere in your code you call a method on the reference of a variable but the variable is a NullPointer. This happens in line 187 as your compiler tells you.
You should now go to line 187 and try to find out which of these variables used there are your NullPointer (perhaps there is only 1 variable used). You can do this by adding some System.out.println() statements to your code to print the references of your variables.
If you found the problem, think where you should initialize the variable with a non-null value. Come back here if you have additional questions.
GregBrannon (July 19th, 2014)