I am trying to pass an arrayList from one activity to another through Intent, but when executing the program the application crashes.
From here I want to send the arraylist
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayList<String> list = new ArrayList<>();
String name = nameInput.getText().toString();
list.add(name);
Intent intent = new Intent(Class2.this, Class1.class);
intent.putExtra("key", list);
startActivity(intent);
}
});
and here I want to receive it
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_names);
ListView myList = findViewById(R.id.list);
ArrayList<String> nameList= (ArrayList<String>) getIntent().getSerializableExtra("key");
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, nameList);
list.setAdapter(adapter);
}
And error is:
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference