I'm making my object parcelable so I can use it with Intents. I'm getting a NullPointerException when, once received, I'm reading my object's content. Most exactly the content of my ArrayList<String>. When iterating over my ArrayList<String> it has null value in all positions, therefore my NullPointerException.
Here's my Parcelable class:
public class PhoneBackup implements Parcelable{
public Integer id;
public Long ts;
public String device_name;
public String device_manufacturer;
public String device_model;
public Integer backup_contacts_count;
public String description;
public ArrayList<String> backup_contacts;
public PhoneBackup()
{
}
public PhoneBackup(Parcel pc){
// backup_contacts = new ArrayList<String>(); // Tried this way, still NPE
id = pc.readInt();
ts = pc.readLong();
device_name = pc.readString();
device_manufacturer = pc.readString();
device_model = pc.readString();
backup_contacts_count = pc.readInt();
description = pc.readString();
// pc.readStringList(backup_contacts); // Tried this way, still NPE
backup_contacts = pc.createStringArrayList();
}
// (getters and setters)
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeLong(ts);
dest.writeString(device_name);
dest.writeString(device_manufacturer);
dest.writeString(device_model);
dest.writeInt(backup_contacts_count);
dest.writeString(description);
dest.writeList(backup_contacts);
}
/** Static field used to regenerate object, individually or as arrays */
public static final Parcelable.Creator<PhoneBackup> CREATOR = new Parcelable.Creator<PhoneBackup>() {
public PhoneBackup createFromParcel(Parcel pc) {
return new PhoneBackup(pc);
}
public PhoneBackup[] newArray(int size) {
return new PhoneBackup[size];
}
};
The NullPointerException is with my backup_contacts. But backup_contacts has the correct size but the containts are all null and as I need to read the contents of backup_contacts using get(position) I receive a null value and not a String.
Question is, why is the Parcelable passing null values to the ArrayList<String> and not he actual Strings? How can I fix it?
PhoneBackupclass which part of a library I developed. I just manually traced the NPE toPhoneBackupthru multiples debugs.