I've got a Problem passing my Arraylist to the next Activity. Here is my Error Code.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference
By my understanding, i am trying to get an Arraylist, which does not exist. I made sure the Arraylist is filled and my Intend values are correct, but im still getting the Error.
Creating the Arraylist
ArrayList<HashMap<String, String>> names = new ArrayList<HashMap<String, String>>();
Creating the intent
Intent intent = new Intent(First.this, Second.class);
intent.putExtra("names", names);
startActivity(intent);
Getting the intent
Intent intent = getIntent();
ArrayList<HashMap<String, String>> usernamen = (ArrayList<HashMap<String, String>>) intent.getSerializableExtra("names");
As said, the Arraylist get's filled correctly via
name = new HashMap<String, String>();
name.put("Name", spielername.getText().toString());
name.put("Gender", gender.getText().toString());
names.add(name);
If someone needs more information, i'll be happy to provide them.
Edit:
gobutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(names.size() < 2){
Toast.makeText(Spieler.this, "No Informations", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(Spieler.this, Games.class);
intent.putExtra("level", level.getProgress());
intent.putExtra("names", names);
startActivity(intent);
}
});
The Button is sending me to the second Activity, so the Array has to be filled.
intenthereIntent intent = getIntent();, should you not use your previously declared intent? Or make sure yourgetIntent()method returns anintent, maybe it is returning null. Use debugger and check ifgetIntent()'s return is not null.intentin anotherActivityis empty? thusgetSerializableExtra("names")this method is called onnullreference?Intent intent = getIntent();in Game Activity is different from null?