List of custom object and Parcelable give NullPointerException

2.1k views Asked by At

I am trying to understand principles of Parcelable in Android and I wrote this simply code to find what is going on when I try to "parcel" a list of custom object - it gives my NullPointerException

Here is my code :

package cz.united121.android.testpupose.Objects.HelperObject;

import android.os.Parcel;
import android.os.Parcelable;

public class MyString implements Parcelable {
private static final String TAG = MyString.class.getName();


String mString;

public MyString(){

}

public MyString(String mString){
    this.mString = mString;
}

public MyString(Parcel in){
    mString = in.readString();
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(mString);
}

public static final Parcelable.Creator<MyString> CREATOR
        = new Parcelable.Creator<MyString>() {
    public MyString createFromParcel(Parcel in) {
        return new MyString(in);
    }

    public MyString[] newArray(int size) {
        return new MyString[size];
    }
};

}

Second class has List of MyString

package cz.united121.android.testpupose.Objects;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import cz.united121.android.testpupose.Objects.HelperObject.MyString;
public class CustomSmall implements Parcelable {
private static final String TAG = CustomSmall.class.getName();


int mId;
String mName;
int mDummy;
List<MyString> mStringList;

public CustomSmall(){
    Log.d(TAG,"CustomSmall()");
    mStringList = new ArrayList<>();
}

public CustomSmall(Parcel in){
    Log.d(TAG,"CustomSmall(Parcel in)");
    mId = in.readInt();
    mName = in.readString();
    mDummy = in.readInt();
    in.readTypedList(mStringList,MyString.CREATOR);
}

public CustomSmall(int mId, String mName, int mDummy, List<MyString> mStringList){
    Log.d(TAG, "CustomSmall(int mId, String mName, int mDummy, List<MyString> mStringList)");
    this.mId = mId;
    this.mName = mName;
    this.mDummy = mDummy;
    this.mStringList = mStringList;
}

@Override
public int describeContents() {
    Log.d(TAG,"describeContents()");
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    Log.d(TAG,"writeToParcel(Parcel dest, int flags)");
    dest.writeInt(mId);
    dest.writeString(mName);
    dest.writeInt(mDummy);
    dest.writeTypedList(mStringList);
}

public static final Parcelable.Creator<CustomSmall> CREATOR
        = new Parcelable.Creator<CustomSmall>() {
    public CustomSmall createFromParcel(Parcel in) {
        Log.d(TAG,"createFromParcel(Parcel in)");
        return new CustomSmall(in);
    }

    public CustomSmall[] newArray(int size) {
        Log.d(TAG,"newArray(int size)");
        return new CustomSmall[size];
    }
};
}

and here is how I test it One activity has this after click:

    MyString myString1 = new MyString("a");
    MyString myString2 = new MyString("b");
    MyString myString3 = new MyString("c");
    MyString myString4 = new MyString("d");

    List<MyString> myStringList1 = new ArrayList<>();
    myStringList1.add(myString1);
    myStringList1.add(myString4);

    List<MyString> myStringList2 = new ArrayList<>();
    myStringList1.add(myString1);
    myStringList1.add(myString2);
    myStringList1.add(myString3);
    myStringList1.add(myString4);

    CustomSmall customSmall_1 = new CustomSmall(1,"First",42,myStringList1);
    CustomSmall customSmall_2 = new CustomSmall(2,"Second",88,myStringList2);

    bundle.putParcelable(CUSTOM_SMALL + "1",customSmall_1);
    bundle.putParcelable(CUSTOM_SMALL + "2",customSmall_2);
    Bundle b = new Bundle();
    Intent toNavigationDrawer = new Intent(MainActivity.this,NavigationDrawerTest.class);
    toNavigationDrawer.putExtras(b);
    startActivity(toNavigationDrawer);

and class NavigationDrawer has this :

    Bundle bundle = getIntent().getExtras();
    CustomSmall customSmall_1 = bundle.getParcelable(CUSTOM_SMALL + "1");
    CustomSmall customSmall_2 = bundle.getParcelable(CUSTOM_SMALL + "2");

but i gives my error :

Unable to start activity ComponentInfo{cz.united121.android.testpupose/cz.united121.android.testpupose.NavigationDrawerTest}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
...
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
...
at cz.united121.android.testpupose.Objects.CustomSmall.<init>(CustomSmall.java:34)
        at cz.united121.android.testpupose.Objects.CustomSmall$1.createFromParcel(CustomSmall.java:64)
        at cz.united121.android.testpupose.Objects.CustomSmall$1.createFromParcel(CustomSmall.java:61)

and CustomSmall.java:34 is line :

 in.readTypedList(mStringList,MyString.CREATOR); in public CustomSmall(Parcel in){

Anyone know the solution ? I am trying to solve this almost 2 days. Big thanks in advance

1

There are 1 answers

1
Anton Kovalyov On BEST ANSWER

You didn't initialized your mStringList list in constructor:

public CustomSmall(Parcel in){
    Log.d(TAG,"CustomSmall(Parcel in)");
    mId = in.readInt();
    mName = in.readString();
    mDummy = in.readInt();
    mStringList = new ArrayList<>();
    in.readTypedList(mStringList,MyString.CREATOR);
}