0

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?

dazito
  • 7,740
  • 15
  • 75
  • 117
  • **ALWAYS** include the logcat in your question if you get exceptions. – Xaver Kapeller Jun 26 '14 at 14:53
  • I understand your comment but the stacktrace would be quite useless here as it was pointing to my app methods with no mention to `PhoneBackup ` class which part of a library I developed. I just manually traced the NPE to `PhoneBackup` thru multiples debugs. – dazito Jun 26 '14 at 15:02

2 Answers2

3

To read a list it should be

backup_contacts = new ArrayList<String>();
in.readList(backup_contacts, null);

Here is the documentation.

The documentation for createStringArrayList says:

Read and return a new ArrayList containing String objects from the parcel that was written with writeStringList(List)

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

I think the problem might be in the writing to the parcel :

dest.writeList(backup_contacts);

should be

dest.writeStringList(backup_contacts);

public final void writeStringList (List val) Added in API level 1

Flatten a List containing String objects into the parcel, at the current dataPosition() and growing dataCapacity() if needed. They can later be retrieved with createStringArrayList() or readStringList(List).

Eran
  • 387,369
  • 54
  • 702
  • 768