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.