Arraylist in parcelable object
Asked Answered
A

2

35

I have seen many parcelable examples so far, but for some reason I can't get it to work when it gets a bit more complex. I have a Movie object, which implements Parcelable. This book object contains some properties, such as ArrayLists. Running my app results in a NullPointerException when executing the ReadTypedList ! I'm really out of ideas here

public class Movie implements Parcelable{
   private int id;
   private List<Review> reviews
   private List<String> authors;

   public Movie () {
      reviews = new ArrayList<Review>();
      authors = new ArrayList<String>();
   }

   public Movie (Parcel in) {
      readFromParcel(in);
   }

   /* getters and setters excluded from code here */

   public void writeToParcel(Parcel dest, int flags) {

      dest.writeInt(id);
      dest.writeList(reviews);
      dest.writeStringList(authors);
   }

   public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>() {

      public MoviecreateFromParcel(Parcel source) {
         return new Movie(source);
      }

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

   };

   /*
    * Constructor calls read to create object
    */
   private void readFromParcel(Parcel in) {
      this.id = in.readInt();
      in.readTypedList(reviews, Review.CREATOR); /* NULLPOINTER HERE */
      in.readStringList(authors);
   }
}

The Review class:

    public class Review implements Parcelable {
   private int id;
   private String content;

   public Review() {

   }

   public Review(Parcel in) {
      readFromParcel(in);
   }

   public void writeToParcel(Parcel dest, int flags) {
      dest.writeInt(id);
      dest.writeString(content);
   }

   public static final Creator<Review> CREATOR = new Creator<Review>() {

      public Review createFromParcel(Parcel source) {
         return new Review(source);
      }

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

   private void readFromParcel(Parcel in) {
      this.id = in.readInt();
      this.content = in.readString();
   }

}

I would be very grateful if someone could just get me on the right track, I have spend quite a bit of time searching for this one !

Thanks in adnvance Wesley

Amias answered 16/1, 2011 at 9:59 Comment(0)
D
38

reviews and authors are both null. You should first initialize the ArrayList. One way to do this is chain the constructor:

public Movie (Parcel in) {
   this();
   readFromParcel(in); 
}
Dissidence answered 16/1, 2011 at 10:13 Comment(1)
THANK YOU SO MUUUUUCH, I wasted 2 days on this :-).Amias
C
15

From the javadocs for readTypedList:

Read into the given List items containing a particular object type that were written with writeTypedList(List)

at the current dataPosition(). The list must have previously been written via writeTypedList(List) with the same object type.

You wrote them with a plain

dest.writeList(reviews);
Clanton answered 16/1, 2011 at 10:18 Comment(3)
Oops, I changed that right before copy pasting my code here. In my project I have this as typed, thanks for noticing it though !Amias
@Clanton : dest.writeList is used for list of type like string,int,boolean etc ??Rugen
Best Answer, Position matters in write and read objects, I have wasted a lot of my time. But your answer save my day. Thanks bro.Landlord

© 2022 - 2024 — McMap. All rights reserved.