This site requires JavaScript, please enable it in your browser!
Greenfoot back
IshaanMishra
IshaanMishra wrote ...

2019/2/4

NullPointerException: problem with ending with gameover

IshaanMishra IshaanMishra

2019/2/4

#
public void GameOver(){
World myWorld=getWorld();
GameOver gameover=new GameOver();
myWorld.addObject(gameover,(int)myWorld.getWidth()/2,(int)myWorld.getHeight()/2);;

}
In my code, I have a spaceship which upon losing health after being hit by asteroids explodes and gets removed from the world. i call the above code in the spaceship's act class. whenever the spaceship explodes, a nullpointer exception gets thrown up at this line and i dont know what to do
myWorld.addObject(gameover,(int)myWorld.getWidth()/2,(int)myWorld.getHeight()/2);;

}
danpost danpost

2019/2/4

#
You are probably removing the actor before calling the GameOver method. Show code around which you have the following line:
GameOver();
IshaanMishra IshaanMishra

2019/2/5

#
This was the code when I called GameOver(). Explode removes the spaceship.
 if(health<=0){
    explode();
    GameOver(); 
     }
I changed it after what you said to
 if(health<=0){
    GameOver(); 
 explode();
     }
and it works now! Thank you so much :)
You need to login to post a reply.