vendredi 13 mars 2015

Trouble Resolving a Null Pointer Exception in Java



I'm a new Java programmer who just encountered his first null pointer exception. I went on a wild goose chase through my classes, but came up with nothing.


I'm following Kilobolt's Java game development tutorial (http://ift.tt/1aU24yT) and I've reached the very end. Unfortunately, I'm getting a null pointer exception in the update function of the game's enemy class. I can post the full source code if needed, but here are the snippets that I think are most pertinent.

The null pointer is coming from



} else if (Math.abs(robot.getCenterX() - centerX) < 5) {


I did some investigation and found that printing robot inside the enemy class prints null. Deeper go, we must. I'm getting robot from a getter function here:



private Robot robot = StartingClass.getRobot();


And the corresponding function in StartingClass is:



public static Robot getRobot() {
return robot;
}


With robot defined as:



private static Robot robot;


I can tell that robot isn't null in StartingClass because I can call the same getter function and print out a result. I don't understand why it doesn't carry over through the getter function.


Thank you.




Aucun commentaire:

Enregistrer un commentaire