|
|
Is there anyone who can tell me why the following code sometimes (not
always) throws a NullPointerException
Using Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
Node currentNode = Universe.space[i][j]; // Have checked and this is never
null.
if (currentNode.hasShip()) { // <-- This line sometimes throws a
NullPointerException
g.drawChars(Labels, 0, 1, i*GridSizeX+GridSizeX/2, j*GridSizeY);
}
if (currentNode.hasAsteroid() ) {
g.drawChars(Labels, 1, 1, i*GridSizeX+1, j*GridSizeY);
}
And here's an abbreviated version of the Node class. The local variable
localShip is never instantiated.
public class Node {
private Ship localShip;
private Asteroid localAsteroid;
boolean hasAsteroid() {
if (localAsteroid!=null)
return true;
else
return false;
}
boolean hasShip() {
if (localShip!=null)
return true;
else
return false;
}
}
Post a reply to this message
|
|