-1

Im working on a 2D platform game for Android and I'm having trouble removing an object.

When the object gets hit I use this code to remove it:

public void removeObject(GameObject object) {
    this.object.remove(object);
}

And this is the main update method (updates all objects):

public void update() {
    synchronized (object) {
        for (int i = 0; i < object.size(); i++) {
            tempObject = object.get(i);
            tempObject.update(object);
        }
    }
}

For some reason when the object gets hit the app crashes with a NullPointerException.

This is the error I get:

java.lang.NullPointerException
    at com.shuster.killtheex.objects.Ex.die
    at com.shuster.killtheex.objects.Ex.update(Ex.java:100)
    at com.shuster.killtheex.state.PlayState.update(PlayState.java:48)
    at com.shuster.killtheex.framework.GameView.update(GameView.java:73)
    at com.shuster.killtheex.framework.GameView.run(GameView.java:119)
    at java.lang.Thread.run(Thread.java:841)

This is the die method:

private void die() {
    playState.removeObject(this);
}

It also appears that this error also happens when I try to add more objects.

Tom
  • 16,842
  • 17
  • 45
  • 54
Gilmor
  • 53
  • 7
  • 1
    Add your stack trace and show where the Exception is happening. It is not clear if it is `object` or `tempObject` or even happening in this block of code. – mkobit Dec 20 '14 at 00:05
  • 1
    Either `this.object` or the `object` you pass as an argument are null, so not initialized. – Martin Dinov Dec 20 '14 at 00:06
  • 1
    probably when you remove an object, the underlying collection isn't rebuilt, just assigns NULL to the removed position. So the update method crashes because the removed position holds a NULL value. – rupps Dec 20 '14 at 00:08

2 Answers2

0

You should just do a check for null on the tempObject, if there is any chance that object.get(i) would return null in some circumstances.

public void update() {
synchronized (object) {
    for (int i = 0; i < object.size(); i++) {
        tempObject = object.get(i);

        if(tempObject != null)
           tempObject.update(object);
    }
}

}

How ever, as the other answer suggests, you first need to find out where the NPE is being thrown. You can do this in 2 ways:

  1. Look at the stackstrace
  2. Use the Debugger in your IDE, and take one line of code at a time, to see when it's throwing the exception

You said that you're doing this to destroy the object, after it's been used. So first of all, make sure you're passing the right objekt into the method. And then again; if there is ANY chance that this object might already have been destroyed by another process, you can do the null check for the object as well:

public void update() {
if(object != null){
 synchronized (object) {
  for (int i = 0; i < object.size(); i++) {
    tempObject = object.get(i);

    if(tempObject != null)
       tempObject.update(object);
  }
}

}

But yes - learn how to use your IDE's debugging tool, and google the exceptions that are being thrown, and try to understand why.

MichaelCleverly
  • 2,473
  • 1
  • 24
  • 33
  • If the nullpointer in question is at the tempObject, of course. – MichaelCleverly Dec 20 '14 at 00:11
  • This is a bad approach to solving the problem. The OP needs to understand which of the references is null and why that happened, and fix what is causing the unexpected nulls. What you are suggesting just hides the real problem. – Stephen C Dec 20 '14 at 00:31
  • I second that. How ever, if there is a possibility that an object might turn out to be null, under some circumstances, it's never bad practise to check for null. But yes, OP - you should debug it, and find out which object is null; then you might be able to track back to see why. – MichaelCleverly Dec 20 '14 at 01:11
  • @user3579809 - See my answer and the other Q&A I linked to. That explains how to solve NPEs. – Stephen C Dec 20 '14 at 03:46
0

For some reason when the object gets hit the app crashes with the null pointer exception.

It happens because you have a null reference (or references) and the where the MPE occurred is not expecting this. Specifically, an NPE could occur in that method for a couple of reasons:

  • If object is null then synchronized(object) will throw an NPE

  • If the object list contains any null references, then you will get an NPE at tempObject.update(...)

UPDATE for the new stacktrace:

The only way you can get an NPE thrown by die (in the version you showed us) is is playState is null.


You haven't provided enough information to figure out why there are unexpected nulls.

Any ideas?

I recommend you follow the approach I described here:

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216