Null Pointer Exception fixed.
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.
Null Pointer Exception fixed.
Last edited by kendraheartt; August 16th, 2012 at 10:36 AM.
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.
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.
First thing I notice is that you are not allocating "theMissile"..need to do a new like you did for "the Bomb".
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...
Please copy and paste here the full text of the error message, It says where the error happens.I'm still getting the null pointer exception.
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.
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....
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.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)
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.
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?
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.something wrong with the way I'm writing my if statemnt
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.
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...
Sorry, what you said makes no sense.comes back as both of them as null
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.
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
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.
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?
Did the location of the NPE change?shield[row][col] didn't show up
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.