Morten Salte
Morten Salte

Reputation: 505

I'm having trouble catching a NullPointerException in android

I have a Service class called UpdaterService. The following is my onStartCommand() method:

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       if(Constants.DEBUG) Log.d(TAG, "onStarted");
       updater = new Updater();
       try {
           updater.start();
       } catch(NullPointerException e) {
           Stuff.showErrorMessage();
       }

       return Service.START_STICKY;
}

updater is a custom object of class Updater which extends Thread. The following is my the exception I'm getting:

08-12 12:19:49.708: ERROR/AndroidRuntime(1995): Uncaught handler: thread Updater exiting due to uncaught exception
08-12 12:19:49.708: ERROR/AndroidRuntime(1995): java.lang.NullPointerException: HostActivity not instantiated
    at com.morten.app.klepprc.services.UpdaterService$Updater.run(UpdaterService.java:135)

This causes my application to crash. At line 135 of UpdaterService.java:

if(application.getHostActivity() == null) {
   throw new NullPointerException("HostActivity not instantiated");
}

How come the exception isn't being handled in my catch block? All feedback appriciated.

Upvotes: 0

Views: 222

Answers (1)

Madushan
Madushan

Reputation: 7468

I assume that your start(); method is non-blocking.

The exception happened in run() method, which is on a different thread ?

Upvotes: 1

Related Questions