Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble Resolving a Null Pointer Exception in Java

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.

like image 627
EWS Avatar asked Jan 25 '26 04:01

EWS


2 Answers

Is complicated to understand the situation with that snippets. But analyzing the situation

I assume that you have a class

class  static StartingClass{

    private static Robot robot;

    public static Robot getRobot() {
        return robot;
    }

}

And in someClass you have

class someClass{
    someMethod(){
//Business logic
        if{
//Business logic x2
        }else if (Math.abs(robot.getCenterX() - centerX) < 5) { //NULLPOINTER

        }
    }
}

if you are getting a nullpointer, and you also says that in that momment robot is null, the problem is clearly that, you can´t execute a null.getCenterX(), obviously that will throw an exception. Also ,you says that in startingclass robot is not null, but don´t make sense that really, becouse if in StartingClass robot is not null, you should get the instance of robot.

We have three possible fixs(depending on the business logic of your app):

1) assuming that robot may be null, we check that robot is not null:

    class someClass{
        someMethod(){
    //Business logic
            if{
    //Business logic x2
            }else if (robot!= null && Math.abs(robot.getCenterX() - centerX) < 5) { //NULLPOINTER

        }
    }
}

2) Assuming that maybe the error is that we are getting the robot before assignement,we get robot exactly before the method execution:

 class someClass{
            someMethod(){
        //Business logic
         private Robot robot = StartingClass.getRobot();
                if{
        //Business logic x2
                }else if (robot && Math.abs(robot.getCenterX() - centerX) < 5) { //NULLPOINTER

            }
        }
    }

3) add to somemethod() at begin{

private Robot robot = StartingClass.getRobot();
if(robot==null){
   return
}
like image 113
Ricardo Umpierrez Avatar answered Jan 27 '26 17:01

Ricardo Umpierrez


Check if the CenterX variable in Robot is initiated before it's used.

like image 23
LeFex Avatar answered Jan 27 '26 18:01

LeFex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!