3

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.

4
  • I do not think Java use pointers at all ?!!! Commented Mar 14, 2015 at 0:38
  • 1
    @KickButtowski Java does have pointers, but they are all internal, and managed by the JVM. You cannot explicitly utilize them in code, but they keep up with object by the run-time. You can however create references to objects. Often, NullPointerExceptions happen because you reference a nonexisiting object. Commented Mar 14, 2015 at 0:44
  • All fields are null until your code initializes them. Clearly, StartingClass.robot starts out as null. It appears your other class is calling StartingClass.getRobot() before StartingClass has actually set its own robot field to a non-null value. We can help you more if you include the code from StartingClass which initializes the robot field. Commented Mar 14, 2015 at 1:06
  • Don't use Pastebin; just put your code here. It'd also help if you pared it down to the specific components that were faulty so there wasn't a whole lot of it. Commented Mar 14, 2015 at 3:31

4 Answers 4

1

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is Java. robot && is not going to compile. And doing a null check will avoid the NullPointerException, but it won't make things work as intended.
Yes, you are right, I´m programming on groovy actually. Yes, i think on that, but maybe the method is executing before the robot is created, and this is the real problem, so checking if the robot exist maybe is a good option.
I added one option more,very similar to the first one, but avoiding to execute all the logic.
Adding in the robot != null fixed the problem. Thank you!
1

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

3 Comments

It is ( private int centerX = 100;) and 100 is the same value that I see when I print out getCenterX() from StartingClass.
I think that you will get another type of exection like that you cannot do minus on null objects, not a nullpointer(at least on groovy normally I get that)
When I made a try catch around the error-causing code it threw a nullpointerexception. I've never coded in groovy, so I can't say if it's similar to Java, though.
1

First, let's notice that you have 2 different fields called robot in 2 different classes. One static in class StartingClass and one non-static in the first class (let's say class X).

Now, since the robot field in class X is defined as follows:

private Robot robot = StartingClass.getRobot();

The StartingClass.getRobot() is called immediately when you create a new object from class X. So you must initialize the robot field in StartingClass before you create a new object from class X.

You could initialize the robot firld in StartingClass like this:

public static void init() {
    try {
        robot = new Robot();
    } catch (AWTException ex) {
        System.err.println("Error");
    }
}

Comments

1

You say: "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 know what you mean by "print out a result". The way you can tell a reference is not null is by actually putting in a null check, which I don't think you did.

You said you "defined" robot by:

private static Robot robot;

Note however that robot is null. It's just a name/handle at this point. It doesn't actually refer to an object yet. So until you assign an object to "robot", calling the getter will just return null.

I took a glance at the Enemy class. It looks like that code always does something like

private static Robot robot = GameScreen.getRobot();

As long as the right-hand side returns a Robot object, "robot" will refer to an actual object. It seems like you may have inadvertently removed that part.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.