I'm having a problem which I can't seem to solve here! I'm developing a simple android game and had pretty much implemented everything, but then decided to add some obstacles in (the game is a lunar lander style game).
Basically, I've created an Asteroid class and am just trying to draw them on the screen. When loading the game the asteroids are drawn, and everything works fine, but if you press the home button or power off and then relaunch the game I get a NPE:
03-04 21:33:21.243: E/AndroidRuntime(10748): FATAL EXCEPTION: Thread-680
03-04 21:33:21.243: E/AndroidRuntime(10748): java.lang.NullPointerException
03-04 21:33:21.243: E/AndroidRuntime(10748): at com.swiss196.LunarLander.GameThread.doDraw(GameThread.java:421)
03-04 21:33:21.243: E/AndroidRuntime(10748): at com.swiss196.LunarLander.GameThread.run(GameThread.java:386)
And it points to the following code:
canvas.drawBitmap(test2.getBitmap(), 120, 120, null);
Which would suggest either the canvas or the test2 object / image that getBitmap returns is null, but prior to this I check that the canvas is not null. And within bitmap, I check the image is not null either:
public Bitmap getBitmap() {
if(asteroidImage == null)
asteroidImage = BitmapFactory.decodeResource(mGameView.getContext().getResources(),
R.drawable.asteroid);
return asteroidImage;
}
Any thoughts?
I'd forgotten to reload some of the needed variables from the bundle, including the images..
Thanks for all your help!