views:

198

answers:

3

I'm making a simple 2d game for the android platform, which works perfectly from version 2.0 and above, but when testing it on a 1.6 device, it crashes immediately. On running the debugger, it seems that I'm getting a null pointer exception in the thread class. I was just wondering if anybody has any ideas as to where the problem might be.

Here's the code for the thread class:

package com.marcusortiz.burnination;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class GameThread extends Thread
{
  private SurfaceHolder sHolder;
  private MainView panel;
  private boolean isRunning;

  public GameThread(SurfaceHolder sHolder, MainView panel)
  {
    this.sHolder = sHolder;
    this.panel = panel;
  }

  public void setRunning(boolean isRunning)
  {
    this.isRunning = isRunning;
  }

  public SurfaceHolder getSurfaceHolder()
  {
    return sHolder;
  }

  @Override
  public void run()
  {
    Canvas canvas;
    while(isRunning)
    {
      canvas = null;
      try
      {
        canvas = sHolder.lockCanvas(null);
        synchronized(sHolder)
        {
          for(Sprite s : panel.getSprites())
          {
            s.update();
          }
          panel.onDraw(canvas);
        }
      }
      finally
      {
        if(canvas != null)
        {
          sHolder.unlockCanvasAndPost(canvas);
        }
      }
    }
  }
}

Maybe there's some compatibility issues with this code that I'm not aware of--I'm very much a beginner when it comes to using threads.

EDIT: here's the requested stack trace

burnination [Android Application]   
  DalvikVM[localhost:8608]  
    Thread [<3> main] (Running) 
    Thread [<15> Binder Thread #3] (Running)    
    Thread [<13> Binder Thread #2] (Running)    
    Thread [<11> Binder Thread #1] (Running)    
    Thread [<17> Thread-9] (Suspended (exception NullPointerException)) 
      GameThread.run() line: 53 
+1  A: 

That's not a stack trace, but it looks like the problem is on line 53, which is what I was trying to get at. It looks like the code you posted probably doesn't like up exactly with what you're running, because line 53 is a closing brace. There's probably a variable on that line that's null.

Adam Crume
That's why I'm having so much trouble debugging this--the code is correct, and the app crashes at the end of the finally block
mportiz08
Also, I'm not sure how to find the actual stack trace in the eclipse debugger if this isn't it..sorry, I'm not super familiar with eclipse yet :P
mportiz08
Try double-checking that the code running on the device is exactly the same as the code in your workspace. I haven't used the Eclipse Android debugger with an actual device, just the emulator, but for that stack traces show up in the LogCat view in the DDMS perspective.
Adam Crume
"the app crashes at the end of the finally block"Well, you know canvas isn't null. Is it possible that sHolder is null?
Slapout
If canvas is non-null then sHolder must be non-null.
Adam Crume
Wow, I didn't know about the DDMS perspective, and it helped alot. The line numbers that it gave in that perspective were much more accurate for some reason even though I never changed the code. From there, I was easily able to find the items that were giving me null pointer exceptions--thanks!
mportiz08
Where did you see the items giving you the null pointer exceptions? I am also seeing a nullpointerexception on a closing brace line. Most frustrating.
dpk
A: 

Hi, I give you some suggestions. 1. Find out which line the crash locate. 2. Check the difference of the APIs in the SDK(1.6 & 2.0) source code. If you can work on Android 2.0, then I don't think there is some problem on your code.

levi
A: 

By default, most debuggers (including Eclipse) break only on "uncaught" exceptions. They do not break on "caught" exceptions, i.e. anything for which a "catch" block applies. Your "finally" block is acting as a catch-all here, since it includes code that must be executed even if an exception was thrown.

When a NullPointerException is thrown by a function in the "try" block, the debugger does not stop, because it's a "caught" exception that you might be trying to ignore. The code in the "finally" block executes, and then the exception is re-thrown. This time there's nobody to catch it, so Eclipse halts. You're left pointing at the closing brace of the "finally" block because that's where the re-throw happens.

If you configure a debug breakpoint for caught+uncaught NullPointerException, you will see the failure at the point of the throw. Also, if you just let it continue and let the thread die, you should see logcat output from the Android default uncaught exception handler indicating the point of the throw.

fadden
thanks, great information!
mportiz08