My ExpenseFB class that implements Parcelable contains a Map of UserFB (which implements Parcelable ):
ExpenseFB:
public class ExpenseFB implements Parcelable { private String id; private String name; private String description; private String whopaidID; private String whopaidName; private Double amount; private Map<String, UserFB> partecipants; // setters and getters... @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(id); dest.writeString(name); dest.writeString(description); dest.writeString(whopaidID); dest.writeString(whopaidName); dest.writeMap(partecipants); } protected ExpenseFB(Parcel in) { id = in.readString(); name = in.readString(); description = in.readString(); whopaidID = in.readString(); whopaidName = in.readString(); in.readMap(partecipants,UserFB.class.getClassLoader()); } public static final Creator<ExpenseFB> CREATOR = new Creator<ExpenseFB>() { @Override public ExpenseFB createFromParcel(Parcel in) { return new ExpenseFB(in); } @Override public ExpenseFB[] newArray(int size) { return new ExpenseFB[size]; } }; }
UserFB:
public class UserFB implements Parcelable{ private String id; private String name; private String email; private Map<String, GroupFB> groups; private Map<String, UserFB> friends; // setters and getters @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(id); dest.writeString(name); dest.writeString(email); dest.writeMap(groups); dest.writeMap(friends); } protected UserFB(Parcel in) { id = in.readString(); name = in.readString(); email = in.readString(); in.readMap(groups,GroupFB.class.getClassLoader()); in.readMap(friends,UserFB.class.getClassLoader()); } public static final Creator<UserFB> CREATOR = new Creator<UserFB>() { @Override public UserFB createFromParcel(Parcel in) { return new UserFB(in); } @Override public UserFB[] newArray(int size) { return new UserFB[size]; } }; }
I want to pass an ExpenseFB object between two actions by adding an ExpenseFB object to the target:
intent.putExtra("id", expenseFB);
When I run getIntent().getParcelableExtra("id") in debug mode in the second step, it throws the following exception when trying to execute the readMap() method on the partecipants map:
... Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map.put(java.lang.Object, java.lang.Object)' on a null object reference
I see that the partecipants card in the first step is full: I think the problem is with the writeMap () method.
Is there a standard or better way to pass a Parcelable object containing a map?
Should I ask for another method to send the Card?
I do not want to use the Serializable object, because I read that they make worse.
source share