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 18 of 18

Thread: Help With Null Pointer Exception

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help With Null Pointer Exception

    Null Pointer Exception fixed.
    Last edited by kendraheartt; August 16th, 2012 at 10:36 AM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help With Null Pointer Exception

    In you error message, the first line should say something like java.lang.NullPointerException.
    The next line should tell you which variable has not been initialized and the line number of your code where the error seems to be.
    You have only shown parts of your code, so it is not easy to pick out the problem without the error message and/or the rest of the code.

    Try finding the problem using the information provided by the error message you get. If you can not find it, post again with the relevant code and error message.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help With Null Pointer Exception

    Look at the line where the NPE occurred and find the variable with the null value. If you can't see what variable is null, add a println just before the line with the NPE and print out the values of all the variables used on the line with the error.
    When you find the variable, backtrack in the code to see why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    First thing I notice is that you are not allocating "theMissile"..need to do a new like you did for "the Bomb".

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help With Null Pointer Exception

    Okay, so I just did the same thing for the missile as I did the bomb, and I'm still getting the null pointer exception. Im pretty sure theres an error with theShield variable, but I cant seem to figure out what...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help With Null Pointer Exception

    I'm still getting the null pointer exception.
    Please copy and paste here the full text of the error message, It says where the error happens.

    If theShield variable has a null value, where in the code is it supposed to get a valid value? Is that code being executed?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help With Null Pointer Exception

    So far you have had 2 NullPointerExceptions, which you have correctd. Now a third one. I seriously suggest you try reading the error message and figuring it out. The message tells you the name of the variable which has not been initialized and the line number where there is the first attempt to use it. First the missile and the bomb, now you suspect the shield....

  8. #8
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help With Null Pointer Exception

    Quote Originally Posted by Norm View Post
    Please copy and paste here the full text of the error message, It says where the error happens.

    If theShield variable has a null value, where in the code is it supposed to get a valid value? Is that code being executed?
    It says Exception in thread "Thread-37" java.lang.NullPointerException
    at objectdraw.Drawbale2D.overlaps (Drawable2D.java:123)
    at Shield.isHit(Shield.java:103)
    at Bomb.run(Bomb.java:87)
    whenever a bomb is shot off by the aliens

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help With Null Pointer Exception

    Exception in thread "Thread-37" java.lang.NullPointerException
    at objectdraw.Drawbale2D.overlaps (Drawable2D.java:123)
    at Shield.isHit(Shield.java:103)
    at Bomb.run(Bomb.java:87)
    In Drawable2D at line 123 (called from Shield line 103) there is a variable with a null value. Look at line 123, find the variable with the null value and then backtrack in the code to find out why that variable does not have a valid value.
    If you can't tell which variable has the null value, add a println before line 123 that prints out the values of all the variables on line 123 so you can see.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help With Null Pointer Exception

    It works only if I isolate each variable (the bomb works if I don't include the || (!shield[row][col].isHidden () && shield[row][col].overlaps (missile)) portion and vice versa for the missile... is there maybe something wrong with the way I'm writing my if statemnt in the isHit method?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help With Null Pointer Exception

    something wrong with the way I'm writing my if statemnt
    If you know that a variable can have a null value, you should check for that first before using the variable in the rest of the if statement.

    Do you know which variable has the null value?
    You need to find the variable with the null value.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help With Null Pointer Exception

    when I include both it comes back as both of them as null if i include both of them in the if statement (both the missile and the bomb), but if i only include one in the if statement it doesn't give a null pointer exception... so I'm confused as to why they work individually but not together...

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help With Null Pointer Exception

    comes back as both of them as null
    Sorry, what you said makes no sense.
    What prints out when you print the values of all the variables on line 123 where the NPE happens?

    Can you post the code for line 123 and the println statement(s) that prints the variables on that line and also what is printed out?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help With Null Pointer Exception

    if ((!shield[row][col].isHidden () && (shield[row][col].overlaps (missile)) || shield[row][col].overlaps (bomb))) is the line where i keep getting the NPE... Sorry but I don't quite understand what you want/how you want me to print out the variables

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help With Null Pointer Exception

    Print out the values of: shield[row][col], row and col
    also print out the contents of the shield array:
    System.out.println("shield="+Arrays.toString(shiel d));
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help With Null Pointer Exception

    Row: 1, 2, 3... 25
    Col: 1, 2, 3.... 25
    shield[row][col] didn't show up when i put in the println statements and
    added what you told me to print i got an error saying the Variable Arrays could not be found... is there something i have to import for that to work?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help With Null Pointer Exception

    shield[row][col] didn't show up
    Did the location of the NPE change?
    Why didn't the value of shield[row][col] print?

    When posting what is printed on the console by the program including the error message, you should copy and paste it here, not type it in. There are probably important details you are missing. By not giving full details on what the code is doing, you are making it very hard to find the problem.

    The Arrays class is in the java util package. You should look in the API doc for that detail.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help With Null Pointer Exception

    Quote Originally Posted by kendraheartt View Post
    ... i got an error saying the Variable Arrays could not be found...
    Paste the error message as it appears and the line of code the error was found on. It sounds like a tweaked line according to how you worded the error message.

Similar Threads

  1. [SOLVED] Getting a null pointer exception.
    By Ouzi in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 16th, 2012, 11:25 AM
  2. [B]Null Pointer Exception[/B]-- Please Help!!!
    By entwicklerin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 24th, 2012, 11:24 AM
  3. Null Pointer Exception?
    By SeanEE89 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 16th, 2011, 06:21 PM
  4. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  5. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM