I’ve been following a tutorial here.
So far i have a Gameview and a Gameloop (a thread) set up alright but i just modified the run() method in my thread and i get this error in logcat:
- ERROR/AndroidRuntime(296): java.lang.NullPointerException
- ERROR/AndroidRuntime(296): at biz.hireholly.tutorial.GameLoop.run(GameLoop.java:76)
Which i’m guessing means line 76 is the problem?
that’s: canvas = this.surfaceHolder.lockCanvas();
I’ve commented this in my code snippet below:
@Override
public void run()
{
Canvas canvas;
Log.d(TAG, "Starting Game Loop");
while (running) {
canvas = null;
//try locking canvas, so only we can edit pixels on surface
try{
canvas = this.surfaceHolder.lockCanvas(); //LINE 76
synchronized (surfaceHolder){
this.gameView.onDraw(canvas);
}
} finally{
//in case of exception,
//surface is not left in an inconsistent state
if (canvas != null){
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
See the whole GameLoop class code here: http://pastebin.com/kfTy9vzY
Andd you can see my GameView class here: http://pastebin.com/BkmnrUPU
(They’re both pretty short)
I’ve been trying to figure it out for myself a few days now with google and just general fiddling but I’ve been unable too, i would very much appreciate anyone’s thoughts on the matter!
The problem is with your constructor in GameLoop.java. The line
this.surfaceHolder = surfaceHolder;should be
this.surfaceHolder = surfaceholder;Note the capital ‘H’ in your original code.If you’re using an IDE, you would’ve seen a warning such as “The assignment to variable surfaceHolder has no effect” on that line.