Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I debug this NullPointer exception?

I'm trying to write an application for Android and when I launch my new activity, I've set break points and found that it runs through my onCreate without any errors, but after that function returns, the debugger says there is a NullPointer Exception (NPE). The problem is, it's not in my code, it says its in ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord) line: 2268 and where the source normally would be (if it were my code) I just get "Source not found." and a button that says "Edit Source Lookup Path".

How can I set up Eclipse so I can debug this problem? I'm sure I'm doing something wrong in my code, but because the exception originates in code I can't see, I have no idea how to figure out where the problem is.

I'm using android-sdk-linux_x86-1.5_r3 and my G1 for this project.

I think my question really has more to do with Android development than using Eclipse. I'm not sure where to find the source for the code that is throwing the exception. Maybe there is a way I can debug it without having that file, but I'm not sure what it would be.

like image 671
Asa Ayers Avatar asked Aug 01 '09 20:08

Asa Ayers


4 Answers

I was wondering this myself, and even though it showed the "caused by" in the LogCat, I wanted the debugger to actually break where the NullPointer exception was thrown (even though it was being caught).

Here's how:

  1. Run -> Add Java Exception Breakpoint
  2. Find NullPointerException (or whatever else you might be debugging (the "Caused by" exception)
  3. Make sure Suspend On Caught Exceptions is checked. Click OK.
  4. You should see a new breakpoint in your view

Try running it now, and the debugger should break where it actually occurs.

like image 131
mikelikespie Avatar answered Nov 09 '22 14:11

mikelikespie


Your assumption should be the NPE is caused by you, not Android. The NPE may pop at ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord) line: 2268, but that means you passed a null pointer to something and it kept on getting passed around until performLaunchActivity actually tried using it and crashed when it found a null pointer.

If we can see the full LogCat, particularly everything in red in the Eclipse LogCat it would help for debugging. I've turned myself inside out looking for an NPE only for someone else to find a very subtle resource initializing line out of place.

like image 26
Will Avatar answered Nov 09 '22 15:11

Will


a button that says "Edit Source Lookup Path"

Hit that button and tell Eclipse where to find the source code for that method. That should help. At least you'll be able to step into it in the debugger and read something more sensible.

like image 2
duffymo Avatar answered Nov 09 '22 14:11

duffymo


Usually, at least with Android, an exception triggers two separate traces. The first one is useless. The second one, noted by "Caused by exception", is the one you want.

And, even if neither stack trace has your code, we might recognize something by seeing the whole thing.

like image 2
CommonsWare Avatar answered Nov 09 '22 16:11

CommonsWare