0

I am quite new to Parcelables and are struggling a bit. I pass an object of class Person which implements Parcelable to another activity using Intent.

That works fine, but this object has instantiated an object from the Employer class, and when trying to access methods from this second object it thows a NullPointerException. I guess it is the Employer class object that is not included in the Parcel.

Any ideas for a good practice to make this work? My actual code is a lot more complicated than this, so I hope I don't have to rewrite the enitre app :-) Any ideas is highly appreciated.

Here is a simplified sample of the code I can not get to work:

In MainActivity.java:

Person client = new Person();

Intent i = new Intent();
Bundle b = new Bundle();
b.putParcelable("person",(Person) client);
i.putExtras(b);
i.setClass(this, SecondActivity.class);
startActivity(i);

In Person.java

class Person implements Parcelable{
    Company employer;
    String lastName;

    Person(){
        lastName = "Smith";
        employer = new Company("Burger King");
    }

    public String getLastName(){
        return lastName;
    }

    public String getEmployer(){
        return employer.getName();
    }

}

In Company.class

class Company {
    String name;

    Company(String companyName){
        name = companyName;
    }

    public String getName(){
        return name;
    }
}

In SecondActivity.java

Bundle b = this.getIntent().getExtras();
if(b!=null){
Person client = b.getParcelable("person");
String lastName = client.getLastName(); //works fine
String employer = client.getEmployer(); //thows NullPointerException
rudder
  • 87
  • 7

1 Answers1

0

I think you control how the parcels are read, written, and the object re-created by implementing the CREATOR field:

"Interface for classes whose instances can be written to and restored from a Parcel. Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface."

I think the approach would be to make Company also Parcelable, or otherwise include the company's data in the Person's writeToParcel() and re-create both ojects in Parcelable.Creator.createFromParcel().

See this link, it has a basic example: http://developer.android.com/reference/android/os/Parcelable.html

Juan
  • 5,525
  • 2
  • 15
  • 26
  • Thanks for your answer. I have included the static field CREATOR and all the other overridden methods as in your basic example link, I just didn't want to clutter up my sample code with it. I have also tried to make the Company class implement Parcelable, but without any luck.What I have not tried is to specifically re-create the "employer" object in the Parcelable.Creator.createFromParcel() in the Person class. Is that the way to go? – rudder May 05 '14 at 13:25
  • In the example the Company object is created and lives inside the Person class. In this example I think re-crating the Company when re-creating Person would be ok. – Juan May 06 '14 at 04:38
  • If Company were a reference to an object living outside of Person, but that Person needs to reference, then it would require aditional analysis. Remember that the objects that are passed as parcelables are different objects (memory space) when read that may even be in different virtual machines (processes). – Juan May 06 '14 at 04:45
  • OK. Thank's guys. By storing the result of employer.getName() in a private field when instanciating the client object and return this field when accessing the getEmployer() would store all needed data in the Parcel. But in my case it is not always possible. I will continue my search :-) – rudder May 06 '14 at 16:06