Saturday, June 30, 2012

Debugging NullPointerException exceptions in Android


Debugging a NullPointerException can be a huge annoyance when debugging an Android application.  I use Eclipse 3.2 for my Android/Java programming.  An Android application by default is a highly event driven multi-threaded program (aka 3-ring circus).  If you find that you are getting a NullPointerException in some weird part of your code or worse in the Android library with your code nowhere to be found in the stack trace, it is most likely due to the exception actually occurring in some other part of your code.  However, the NullPointerException doesn’t get raised in the debugger until  some background event or other thread occurs making it hard to figure out where your code went wrong.  The two main suggestions I found in this area on this wonderfully helpful StackOverflow post are:
1) Check Eclipse’s LogCat to see what part of your code was running at the time of the exception.
2)  Execute the following steps to make sure “Suspend On Caught Exceptions” is active.
  • Run -> Add Java Exception Breakpoint
  • Find NullPointerException (or whatever else you might be debugging , that is, the “Caused by” exception)
  • Make sure Suspend On Caught Exceptions is checked. Click OK.
  • You should see a new breakpoint in your view

  • Read the full post for further comments and details.

    No comments:

    Post a Comment