Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null pointer exception

 
Stratos Geroulis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i have the following code:

this goes through compilation but when i run it i get an exception NullPointerException in lines 8 and 15. why that happens??? anyone?
thanks in advance.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must assign the reference array1[] and array2[] to an array object. Like this

What happens in your example is that you are trying to assign a value to an object that is not there.
 
Greg T Robertson
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you never "new" the arrays - you could add a constructor to do that e.g.
(SOME_NUM represents some number that you wish to use for your arrays. I would actually suggest using a final static int for this and updating your loop to say for (int i = 0; i < Game1.array1.length; i++) (or if you wnated to be really careful for (int i = 0; Game1.array1 != null && i < Game1.array1.length; i++) ))

OR you could change the declaration of these arrays to be

OR you could do it just in time as in

but the bottom line is that somewhere or other you need to "new" the array(s).
On another note - it's typical java convention for method names to start with lower case - same with variable names.
hope that helps
 
Stratos Geroulis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks gyus ur life savers!!! i'll drink to that!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic