Results 1 to 10 of 10
- 02-07-2010, 07:19 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
WHY is it not def? PLZ HELP IT IS VERY SIMPLE!!
ok I have an obj called Cell, and i made a 2D array, and I try to rest eveycell,
What is wrong ???
now, I have in my main :Java Code:package thegame; class Cell { boolean occupied = false; char own = ' '; } /** * * @author joseph */ class Queendom { public static final int MAX_LENGHT = 400; Cell [][]Board = new Cell[400][400]; private int lenght = 0; public void reset(){ for(int i=0;i<lenght;i++){ for(int j=0;j<lenght;j++){ Board[i][j].occupied = false; Board[i][j].own = ' '; } } }
But the when I cal the Q.reset(), and it look at the Board[i][j].occupied = false; it fail, and says:Java Code:public class The_Game { public static void main(String[] args) { // TODO code application logic here Queendom Q = new Queendom(); Q.reset(); } }
Exception in thread "main" java.lang.NullPointerException
at thegame.Queendom.reset(Queendom.java:33)
at thegame.The_Game.main(The_Game.java:17)
PLZ help me on this !!!!
Thanks a lot
- 02-07-2010, 07:27 AM #2
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
I tried to compile your codes, but the compiler didnt throw me any NullPointerException... are you sure that it is your problem?
- 02-07-2010, 07:27 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
You don't actually say what line 33 of Queendom.java is!
I'll assume that it is "Board[i][j].occupied = false;" You will get a NullPointerException if Board[i][j] is null. Where did you think Board[i][j] was assigned a nonnull value? (for any i/j).
- 02-07-2010, 07:28 AM #4
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
ya did you try to run it?
b/c it does compile, but could you run it too?
@pbrockway2:
That is it; I didnt access/ use any elements in Board yet, I just want to reset all the occupied values to false.Last edited by just_java; 02-07-2010 at 07:54 AM.
- 02-07-2010, 07:49 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
could any of you guys run this simple program?:rolleyes:
Last edited by just_java; 02-07-2010 at 07:59 AM.
- 02-07-2010, 08:47 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
There's really no reason to run the program (and to do that we would have to add the bits you have left out). There's no reason because you have already told us what happens: you get a NullPointerException when you try and access the array elements.
By the way, you are accessing the array elements when you set occupied to false. This isn't a nit-pick, it's a very important point. The line:
Board[i][j].occupied = false;
means "I want to access the Board array at element i/j, take the Cell which is there and set its occupied field to false". Now the runtime complains because Board[i][j] is null and you can't set the occupied field of something that doesn't exist.
The answer to the question I asked before is that - at the moment - you are not assigning the elements of the Board array nonnull values. And you have to do that to avoid the NPE. Probably in the Queendom constructor, or in the code that alters lenght.
-----
It really doesn't help to post code that doesn't correspond with the runtime stack trace. No way is the NPE occurring on line 33. And the reset() method itself doesn't do anything unless something is happening to lenght in code that you haven't posted.Last edited by pbrockway2; 02-07-2010 at 08:51 AM.
- 02-07-2010, 08:51 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 02-07-2010, 08:56 AM #8
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
THANKS EVERYONE;
I reset length outside:
So indeed, length is 400;
So How should I fix it?
Thanks again
- 02-07-2010, 09:01 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
By assigning some nonnull values to the array elements.
- 02-07-2010, 09:01 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Simple Help
By Andy_M in forum New To JavaReplies: 6Last Post: 01-09-2010, 09:10 PM -
Very simple q
By fresh83 in forum New To JavaReplies: 2Last Post: 12-26-2009, 10:06 AM -
New simple application using a simple database
By webbusiness23 in forum New To JavaReplies: 9Last Post: 08-03-2009, 02:55 AM -
Help with a very simple method for a very simple beginner.
By cakeman in forum New To JavaReplies: 2Last Post: 05-04-2008, 05:27 PM -
simple GUI
By dim_ath in forum New To JavaReplies: 3Last Post: 01-07-2008, 03:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks