1

I'm facing a litte problem.

I'm trying to pass an playlist object to my media player service.

In its onStartCommand, I'm getting a Nullpointer Exception when trying to call extras.get() or extras.getParcelable().

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    L.d(this, "Service started");
    Bundle extras = intent.getExtras();//Not null
    if (extras == null) {
        stopSelf();
        return 0;
    }
    L.d(this, "Extras was " + (extras == null ? "null" : "not null"));
    defaultPlaylist = extras.getParcelable("playlist");//Nullpointer
    currentSongNumber = extras.getInt(INITIAL_SONG_NUMBER);//Nullpointer

    registerReceiver(getApplicationContext());
    new NotificationPlayerControl(getApplicationContext());
    NotificationPlayerControl.update(defaultPlaylist.getSong(currentSongNumber));
    return START_STICKY;
}

extras is not null.

What is going on there?

Edit: it seems that the error occures when I´m trying to write my parcelable object to the Bundle. I excluded it and now it runs well.

Maybe the Stacktrace was just a bit misleading

Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136
  • Have you tried directly calling `intent.getParcelableExtra("playlist")`? – u3l Mar 05 '14 at 15:33
  • yes,still the same problem. I also tried getInt which doesn´t work neither – Marian Klühspies Mar 05 '14 at 15:36
  • Okay, it's possible that `playlist` is an incorrectly spelt key. Check if both the key when you pass the object and when you recieve it in your Service are the same. If no mapping of the type for the key exists, then it will return null. – u3l Mar 05 '14 at 15:38
  • But it should not crash the app with a nullpointer exception – Marian Klühspies Mar 05 '14 at 15:39
  • Oh right... I'm not quite sure why you're getting a npe. – u3l Mar 05 '14 at 15:44
  • I just tried using different keys for when you're passing the Parcelable through an intent and when you're taking it out. A NullPointerException **was** thrown. Make sure your keys are the same (the case matters). – u3l Mar 05 '14 at 15:52
  • Can you post the exact changed code that fixed it? – u3l Mar 05 '14 at 15:56
  • No. I havent fixed it yet, sorry. There is something wrong with my Parcelable.Creator – Marian Klühspies Mar 05 '14 at 15:58

1 Answers1

0

you are checking, but still accessing it after the check:

if (extras == null || defaultPlaylist == null) {
        stopSelf();
        return;
 } 
    L.d(this,"Extras was "+(extras==null?"null":"not null"));
   extras.getParcelable("playlist");
Blackbelt
  • 156,034
  • 29
  • 297
  • 305