Custom Objects List and Parcelable gives NullPointerException
I am trying to understand the principles of Parcelable in Android and I wrote this simple code to see what happens when I try to "send" a list of a 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];
}
};
}
The second class has a 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 this is how I test it One action has this after a 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 the NavigationDrawer class has the following:
Bundle bundle = getIntent().getExtras();
CustomSmall customSmall_1 = bundle.getParcelable(CUSTOM_SMALL + "1");
CustomSmall customSmall_2 = bundle.getParcelable(CUSTOM_SMALL + "2");
but I am giving 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 the line:
in.readTypedList(mStringList,MyString.CREATOR); in public CustomSmall(Parcel in){
Does anyone know a solution? I've been trying to solve this for almost 2 days. Thank you very much in advance
+3
source to share
1 answer
You haven't initialized your mStringList
list in the 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);
}
+5
source to share