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?

2
  • ALWAYS include the logcat in your question if you get exceptions. Commented Jun 26, 2014 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. Commented Jun 26, 2014 at 15:02

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.