0

I'm using AndroidAnnotations and I have a few IntentServices. I sometimes get this error (happens on all Services, not just the one below):

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getAction()' on a null object reference
       at xxxx.DataLoaderService_.onHandleIntent(DataLoaderService_.java:51)
       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)

But I can't check if Intent is null because DataLoaderService_ is generated and not editable.

How can I solve this problem?

EDIT: This is an example of how this service gets called:

DataLoaderService_.intent(MyApplication.getContext()).loadMyData().start();

And this is the service (generated):

@Override
    public void onHandleIntent(Intent intent) {
        DataLoaderService_.super.onHandleIntent(intent);
        String action = intent.getAction();
        if (ACTION_LOAD_MY_DATA.equals(action)) {
            super.loadMyData();
            return ;
        }
}

       public static class IntentBuilder_
            extends ServiceIntentBuilder<DataLoaderService_.IntentBuilder_>
        {
        public DataLoaderService_.IntentBuilder_ loadMyData() {
                    action(ACTION_LOAD_MY_DATA);
                    return this;
                }
    }
fanboy555
  • 291
  • 2
  • 7
  • 20
  • Please post the code that invokes this IntentService. – Egor Jun 07 '16 at 12:54
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Harshad Pansuriya Jun 07 '16 at 13:05
  • @Ironman I know how to fix the error inside normal class, but this is happening inside a generated class. – fanboy555 Jun 07 '16 at 13:06

1 Answers1

-2

your intent has a null value. use this code instead:

if (intent != null) {
 String action = intent.getAction();
        if (ACTION_LOAD_MY_DATA.equals(action)) {
            super.loadMyData();
            return ;
        }
    }
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • How can I can do this inside a generated class that I can't change? That's what I'm asking. – fanboy555 Jun 07 '16 at 13:05
  • i do not know your code. so, read this : http://stackoverflow.com/questions/8421430/reasons-that-the-passed-intent-would-be-null-in-onstartcommand and this – Vyacheslav Jun 07 '16 at 13:13