How can I debug this NullPointer exception?

后端 未结 7 1612
南方客
南方客 2020-12-28 19:49

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

相关标签:
7条回答
  • 2020-12-28 19:53

    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.

    0 讨论(0)
  • 2020-12-28 19:55

    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.

    0 讨论(0)
  • 2020-12-28 19:55

    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.

    0 讨论(0)
  • I haven't worked with Android, but Eclipse should be able to show you the call stack.

    Look at the point where the program exits your code and enters someone else's, and make sure all your variables correct (non-null, contain the "right" data, etc.).

    0 讨论(0)
  • 2020-12-28 20:02

    Even if it's happening within the Android code, you should still be able to narrow down at what point it's happening and which of your variables it doesn't like. Yeah, vague answer, not much else to say. Or download the source and tell Eclipse where it is.

    0 讨论(0)
  • 2020-12-28 20:10

    If you want to see the location where the exception happens without downloading the Android source code, there is a nice plugin for eclipse called GrepCode. It searches and displays the source code of popular java projects online.

    0 讨论(0)
提交回复
热议问题