This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Null pointer exception" Watch "Null pointer exception" New topic

Null pointer exception

Stratos Geroulis

Joined: Mar 14, 2004
Posts: 6
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

Joined: Jul 21, 2003
Posts: 522
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.

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
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

Joined: Mar 14, 2004
Posts: 6
thanks gyus ur life savers!!! i'll drink to that!
I agree. Here's the link:
subject: Null pointer exception
It's not a secret anymore!