6

I'm seeing a crash in Crashlytics:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference at com.myapp.APKOfferQueueManagerIntentService.onHandleIntent(SourceFile:71) at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.os.HandlerThread.run(HandlerThread.java:61)

How can it be that an IntentService is being triggered with a null intent?

JY2k
  • 2,879
  • 1
  • 31
  • 60

1 Answers1

7

An IntentService may be stopped like every other service, but it will be restarted by "the system" until onHandleIntent() has finished its work. In this case, the documentation says the intent parameter

... may be null if the service is being restarted after its process has gone away

To always get the original Intent as a parameter, use

setIntentRedelivery(true);

in the constructor of the IntentService.

Note also that

If multiple Intents have been sent, only the most recent one is guaranteed to be redelivered.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
  • This sounds like the solution. Is there a way I can simulate an intentService being abruptly killed and then called again? I'd like to verify the solution. – JY2k Jun 23 '16 at 08:06
  • @JY2k - I'd try something like a [task killer app](https://github.com/commonsguy/cw-omnibus/tree/master/Tasks/Nukesalot) - or maybe this [SO post by Xavi Gil](http://stackoverflow.com/a/26253635/5015207) will be useful – Bö macht Blau Jun 23 '16 at 11:43