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.

Results 1 to 3 of 3

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Exception in thread "main" java.lang.NullPointerException

    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.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    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.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    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.

  4. The Following User Says Thank You to Cornix For This Useful Post:

    GregBrannon (July 19th, 2014)

Similar Threads

  1. "Exception in thread "main" java.lang.NullPointerException" help?
    By redstripes in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 28th, 2014, 08:59 AM
  2. I get the error Exception in thread "main" java.lang.NullPointerException?
    By jeremy28 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 3rd, 2013, 11:13 PM
  3. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  4. Replies: 5
    Last Post: December 9th, 2012, 02:25 PM
  5. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM

Tags for this Thread