Avoid uncaught NullPointerException at Android app launch

While we all strive to avoid any kind of exceptions in software, we all make mistakes. One exception to be particularly mindful of is causing an uncaught NullPointerException during the app launch process on Android pre-2.2 (meaning the initial onCreate+onStart+onResume chain). The reason to avoid such a situation is that your app will crash, of course, but furthermore, the Android OS will notice that launching your app failed and will automatically try to launch it again — ad infinitum.

The end result is that the phone gets into an infinite loop of launching your app which will crash, only to be launched again to crash, etc. The only recourse for the user is to reboot the phone or for the developer to reboot the emulator. Unpleasant, to say the least. If you ever run into this situation, check for a single point of failure during the app creation and launch process and ignore the infinite loop of exceptions. Your app will be at fault for the exception in the first place, the but infinite loop part will be out of your control.

Thankfully, my discovery of this particular quirk was not with any published app, but only with one I’m in the process of developing and testing. I’m glad the infinite looping issue has already been resolved in Android 2.2, but if you still target earlier versions with your app, pay special attention to your startup code.

Leave a Reply

Your email address will not be published.