0

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.

9
  • You are re declaring your intent here Intent intent = getIntent();, should you not use your previously declared intent? Or make sure your getIntent() method returns an intent, maybe it is returning null. Use debugger and check if getIntent()'s return is not null. Commented Feb 20, 2019 at 4:30
  • maybe that intent in another Activity is empty? thus getSerializableExtra("names") this method is called on null reference? Commented Feb 20, 2019 at 4:32
  • Could you show the code you used in the other activity? Commented Feb 20, 2019 at 4:34
  • Well, the first Activity is basically there. I've got two Edittext and the Intent in a Button. The Array is correctly filling, i've checked that via a simple Toast. Commented Feb 20, 2019 at 4:39
  • Intent intent = getIntent(); in Game Activity is different from null? Commented Feb 20, 2019 at 4:46

2 Answers 2

1
 ArrayList<HashMap<String,String>> names = new ArrayList<>();

 HashMap<String,String> map = new HashMap<>();

 map.put("Name", "Spieler");

 map.put("Gender", "male");

 names.add(map);

Send the data with Intent

Bundle bundle = new Bundle();

bundle.putSerializable("keyName",names);

Intent i=new Intent(v.getContext(), SecondActivity.class);

i.putExtras(bundle);

startActivity(i);

Get the data from Intent

Intent intent = this.getIntent();

Bundle bundle = intent.getExtras();

Log.e("SecondActivity","yourArrayList--"+(ArrayList<HashMap<String,String>>)bundle.getSerializable("keyName"));
Sign up to request clarification or add additional context in comments.

1 Comment

@Specializedx3, Mark this correct answer and upvote it so that It will help other users as well. Happy to help you
0

You need to serialize the value first, with either using Bundle:

Bundle bundle = new Bundle(); 
bundle.putSerializable("names", namen); 
intent.putExtras(bundle);

or casting the list with Serializable:

intent.putExtra("names", (Serializable) names);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.