0

I have a custom object and an Array List, I get a null exception when i try to retrieve a parcelable Array List. The code from the custom object:

public class Patient implements Parcelable {

private String fullName;
private String id; // this is the Key of the Table Patient
private String address;
private int age;
private String telephone;
private String Doctor;
private String  lastVisit;
private String healthStatus;

//constructor
public Patient(String fullName, String id, String address, int age, String telephone, String doctor, String lastVisit, String healthStatus) {
    this.fullName = fullName;
    this.id = id;
    this.address = address;
    this.age = age;
    this.telephone = telephone;
    Doctor = doctor;
    this.lastVisit = lastVisit;
    this.healthStatus = healthStatus;
}

//Key
public String getId() {
    return id;
}

public String getAddress() {
    return address;
}

public int getAge() {
    return age;
}

public String getTelephone() {
    return telephone;
}

public String getDoctor() {
    return Doctor;
}

public String getFullName() {

    return fullName;
}

public String getLastVisit() {
    return lastVisit;
}

public String getHealthStatus() {
    return healthStatus;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(age);
    dest.writeString(fullName);
    dest.writeString(address);
    dest.writeString(id);
    dest.writeString(telephone);
    dest.writeString(Doctor);
    dest.writeString(String.valueOf(lastVisit));
    dest.writeString(healthStatus);
}

public static final Parcelable.Creator<Patient> CREATOR = new Parcelable.Creator<Patient>() {
    public Patient createFromParcel(Parcel in) {
        return new Patient(in);
    }

    public Patient[] newArray(int size) {
        return new Patient  [size];
    }

};

private Patient(Parcel in) {
    age = in.readInt();
    fullName = in.readString();
    address = in.readString();
    id = in.readString();
    telephone = in.readString();
    Doctor = in.readString();
    fullName = in.readString();
    healthStatus = in.readString();
    lastVisit =  in.readString();
}

}

The part of code from the first Activity:

 if (user != null) {
       Intent intent = new Intent(logIn.this, MainActivity.class);
       if(mPatientList != null){
            for(int i = 0 ; i < mPatientList.size(); i ++){
               if(stUserName.equals(mPatientList.get(i).getDoctor())){
                    UserList.add(mPatientList.get(i));
               }
            }
       Bundle bundle = new Bundle();
       bundle.putParcelableArrayList("patientList", UserList);
       intent.putExtras(bundle);
       startActivity(intent);
       finish();
 }

and the part of code from the next Activity where I try to retrieve the list :

if(savedInstanceState != null) {
    patientList = savedInstanceState.getParcelableArrayList("patientList");
}

For some reason the List from the Next Activity is null and i get an Exception when trying to use it.

I follow the documentation and i cannot figure out where is the wrong part. Also the lists in the First activity are full with data.

Thanks for the help!

VasFou
  • 1,561
  • 1
  • 22
  • 39

1 Answers1

3

You are putting the list in the intent but you are trying to retrieve it from savedInstanceState in the second activity. You need to retrieve the list from the intent.

patientList = getIntent().getParcelableArrayList("patientList"); // May require a cast if you partient list is a specific type
Naveed
  • 2,942
  • 2
  • 25
  • 58