Flyentology
Flyentology

Reputation: 121

getSerializableExtra Null Pointer Exception

I'm trying to send object from service class to MainActivity using intents but I always receive that exception. My service class intent.

private List<Song> songsList;
@Override
public void onCreate(){
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.putExtra("songsList", (Serializable) songsList);
    startActivity(intent);
}

My activity receiving intent

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    songsList = findViewById(R.id.songsList);
    songsList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    startService(new Intent(this, MediaPlayerHolder.class));

    baseSongList = (ArrayList<Song>)getIntent().getSerializableExtra("songsList");
    songsListView.addAll(baseSongList);
    arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, songsListView);
    songsList.setAdapter(arrayAdapter);
    }

Exception happens just when I try to cast extras to ArrayList.

07-09 17:05:44.079 2113-2113/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.simplemusicplayer, PID: 2113
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplemusicplayer/com.simplemusicplayer.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2785)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1532)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6337)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
 Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
    at java.util.ArrayList.addAll(ArrayList.java:562)
    at com.simplemusicplayer.MainActivity.onCreate(MainActivity.java:86)
    at android.app.Activity.performCreate(Activity.java:6858)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2785) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1532) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:163) 
    at android.app.ActivityThread.main(ActivityThread.java:6337) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770) 

Upvotes: 0

Views: 840

Answers (1)

Man
Man

Reputation: 2817

Initialize songlist from service if you haven't.

Make your Song Parcelable and put List using

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.putParcelableArrayListExtra("songsList", songsList);
    startActivity(intent);

And get in activity using

baseSongList = (ArrayList<Song>)getIntent().getParcelableArrayListExtra("songsList");

Upvotes: 1

Related Questions