my dog learned polymorphism
The moose likes Beginning Java and the fly likes Null Pointer Exception Error - .contains(p) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Null Pointer Exception Error - .contains(p)" Watch "Null Pointer Exception Error - .contains(p)" New topic

Null Pointer Exception Error - .contains(p)

capp luckett
Ranch Hand

Joined: Dec 07, 2000
Posts: 33
I am now getting a "NullPointerException at Game.checkBox(". I believe that this due to the statement
"if (rect [i].contains(p))
grid = i + 1;". I am not sure whether I need another "this."
on what?
Still trying for that deadline.
My code follows:

[This message has been edited by Marilyn deQueiroz (edited October 09, 2001).]

Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
It would be considerate if you put [ code][ /code] tags in your posts.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
matt hooker
Ranch Hand

Joined: Jul 26, 2001
Posts: 46
Uhhh, what have you done to the code ? You have massacred it. I cannot help you if you do not listen to the advice...... you now seem to have braces (curly brackets) floating randomly all over your code.

Its not what you do, its the way you say you've done it.
Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
You also seem to have declared variable "rect" of type Rectangle [] TWICE !
Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
In the init() method you do not need to declare the type of variable "rect" again, only that it has 9 entries, thus:

You seem to be numbering the grids 1 to 9, and then using this number as an index into the array "move[]". Java numbers the array 0 to 8! So no wonder you are getting all kinds of errors.
In method checkBox() decide how you are going to set up the variable "grid".
It's also worth mentioning that when looping through an array it isn't good practice to use a number (e.g. 9), use "arrayName.length" (e.g. rect.length) instead. That way if the number of entries in an array ever changes the existing code will still check all entries and you won't be in danger of 'missing' a line of code that you should have changed.
Perhaps a quick refresher through the online Java tutorial would be of benefit here. Remember it's a lot easier to write good code to start with than to debug bad code later on.
Good luck.
I agree. Here's the link:
subject: Null Pointer Exception Error - .contains(p)
It's not a secret anymore!