Null Pointer exception starting IntentService

10,437

Solution 1

If you're going to override onCreate() in your IntentService, then make sure you call super.onCreate() in it. That seems to quite likely be your problem.

Solution 2

Not sure if it is the same problem, but I was using an intentService also, and I was having trouble using

context = getApplicationContext();

or context = getBaseContext(); I wasn't overriding onCreate so the previous solution may be the solution for you, I was working inside "onHandleIntent"

I would get an immediate exception with the first, and a exception later when I tried using the 2nd.

I ended up realizing that the Intentservice is itself a subclass of Context, so I substituted "this" wherever I needed an instance of "Context".

This solved my problem.

Share:
10,437
Trev
Author by

Trev

Updated on June 03, 2022

Comments

  • Trev
    Trev about 2 years

    I've got an IntentService that I'm trying to start. When I do, it spits out this:

    java.lang.RuntimeException: Unable to start service com.pec.testapp.service.NewsService@406bd940 with Intent { cmp=com.pec.testapp/.service.NewsService }: java.lang.NullPointerException
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2173)
        ... (omitted for brevity)
    Caused by: java.lang.NullPointerException
        at android.app.IntentService.onStart(IntentService.java:110)
        at android.app.IntentService.onStartCommand(IntentService.java:118)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2160)
        ... 10 more
    

    I have googled this and looked at as many similar StackOverflow questions as I could find. However, there are a couple of subtle differences that I can't wrap my head around. First of all, there aren't any of my classes referenced in the exception. Secondly, similar questions have been fixed by changing the context or double checking to make sure it's not null.

    I have code to check that isn't the case:

    public Context context;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
    
        if(context == null)
            Log.d("PECAPP","Context is null");
    
        setContentView(R.layout.news_layout);
    
        ...Omit button code...
        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                Intent i = new Intent(context, NewsService.class); // also tried NewsActivity.this
                if( i != null) // I know that this should never happen but I'm at a loss...
                    startService(i); // I've also tried this with context.startService(i)
            }
        });
    
    }
    

    My IntentService is modelled after the google docs. Simply a constructor with an onHandleIntent method.

    public NewsService() {
        super("NewsService");
    }
    
    ...omit onCreate() and onDestroy() since they haven't been implemented yet...
    
    @Override
    protected void onHandleIntent(Intent intent) throws IllegalArgumentException {
        Log.d("PECAPP","Got here..."); // I never actually got here...
        if(intent == null) Log.d("PECAPP","INTENT IS NULL");
        ...omit rest of code...
    }
    

    So my question is this: Where is this exception coming from and is there something I can do different to avoid it? My google-fu hasn't failed me in the past, so hopefully this isn't one of those painfully obvious answers. Also, if there are things that can be done better or are just plain ugly, constructive criticism is always appreciated.

    I put the full exception, NewsActivity, and NewsService on pastebin in case I left something out. http://pastebin.com/mR9Sykrq

  • Jarett Millard
    Jarett Millard about 11 years
    Usually if you forget this in, say, an Activity, it will remind you when it crashes, but apparently not here.
  • Yasir
    Yasir over 9 years
    Was facing same issue as yours. Many Thanks.. You saved me a lot of time
  • kAmol
    kAmol almost 9 years
    I'm getting null value for appContext Context appContext = getApplicationContext().