• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null Pointer Exception Error - .contains(p)

 
capp luckett
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am now getting a "NullPointerException at Game.checkBox(Game.java:211)". 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
Sheriff
Posts: 9061
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be considerate if you put [ code][ /code] tags in your posts.
 
matt hooker
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Colin Kenworthy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also seem to have declared variable "rect" of type Rectangle [] TWICE !
 
Colin Kenworthy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic