EDIT I'm adding a pastebin of my source code to make it easier to look through my code.
startingclass [http://pastebin.com/Q7w3pbC5]
robot [http://pastebin.com/BbZRFM5K]
enemy [http://pastebin.com/N98Ly1uY]
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://www.kilobolt.com/game-development-tutorial.html) 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.
NullPointerExceptions happen because you reference a nonexisiting object.StartingClass.robotstarts out as null. It appears your other class is calling StartingClass.getRobot() before StartingClass has actually set its ownrobotfield to a non-null value. We can help you more if you include the code from StartingClass which initializes the robot field.