user3155478
user3155478

Reputation: 1055

Receive ArraylistObject getParcelableExtra - NullPointerException

I cannot figure out how to send and receive and arraylist-object between two categories in android. I get A NullPointerException in activity B.

Activity A

 private ArrayList <String[][]> stringObjects2D = new ArrayList <String[][]> () ; 
 Intent intent = new Intent(MainActivity.this, CalculationsActivity.class); 
 intent.putExtra("strObj2D", stringObjects2D );

Activity B

 private ArrayList <String[][]> stringObjects2D;
 stringObjects2D = getIntent().getParcelableExtra("strObj2D");

 // just a check test if something is here. Here I get the NullPointerException
 System.out.println("size = " + stringObjects2D.size());

Would be extremely greatful if someone know how to solve this issue.

thanks!!!

Upvotes: 0

Views: 643

Answers (3)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

You can create a class that implements Parcelable and use that class to pass to the intent to insure that you are passing a parcelable object.

sample:

UPDATE:

    public class Sample implements Serializable {
    private ArrayList<String[][]> stringObjects2D; 
    public Sample() {
                    stringObjects2D = new ArrayList <String[][]> ();
        String [][] s = {{"1","1"},{"1","1"},{"1","1"},{"1","1"},{"1","1"}};
        stringObjects2D.add(s);
        stringObjects2D.add(s);
        stringObjects2D.add(s);
        stringObjects2D.add(s);
    }
    public ArrayList <String[][]> getArray(){
        return this.stringObjects2D;
    }

}

Use in:

Sample s2 = new Sample();
        Intent intent = new Intent(MainActivity.this, SecondActivity.class); 
        intent.putExtra("strObj2D", s2);
        startActivity(intent);

second activity:

 Sample stringObjects2D;
     stringObjects2D = (Sample) getIntent().getSerializableExtra("strObj2D");

     // just a check test if something is here. Here I get the NullPointerException
     System.out.println("size = " + stringObjects2D.getArray().get(0)[0]);

Upvotes: 1

faylon
faylon

Reputation: 7450

The reason is that the String[][] is not Parcelable. Please use ArrayList<ArrayList<String>> instead.

You can also implement your own Parcelable class to store these data if you think the nested type is too long. Check this question for implementation details.

Upvotes: 1

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Use

stringObjects2D = (ArrayList<String[][]>) getIntent().getSerializableExtra("strObj2D");

instead

stringObjects2D = getIntent().getParcelableExtra("strObj2D");

Upvotes: 0

Related Questions