title: Read & writing arrays of Parcelable objects
body: <p>I have following class which reads and writes an array of objects from/to a parcel:</p>
  
  <pre><code>class ClassABC extends Parcelable {
      MyClass[] mObjList;
  
      private void readFromParcel(Parcel in) {
          mObjList = (MyClass[]) in.readParcelableArray(
                  com.myApp.MyClass.class.getClassLoader()));
      }
  
      public void writeToParcel(Parcel out, int arg1) {
          out.writeParcelableArray(mObjList, 0);
      }
  
      private ClassABC(Parcel in) {
          readFromParcel(in);
      }
  
      public int describeContents() {
          return 0;
      }
  
      public static final Parcelable.Creator&lt;ClassABC&gt; CREATOR =
              new Parcelable.Creator&lt;ClassABC&gt;() {
  
          public ClassABC createFromParcel(Parcel in) {
              return new ClassABC(in);
          }
  
          public ClassABC[] newArray(int size) {
              return new ClassABC[size];
          }
      };
  }
  </code></pre>
  
  <p>In above code I get a <code>ClassCastException</code> when reading <code>readParcelableArray</code>:</p>
  
  <blockquote>
    <p>ERROR/AndroidRuntime(5880): Caused by: java.lang.ClassCastException: [Landroid.os.Parcelable;</p>
  </blockquote>
  
  <p>What is wrong in above code? While writing the object array, should I first convert the array to an <code>ArrayList</code>?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Is it OK to convert an Object array to an <code>ArrayList</code> and add it to parcel? For instance, when writing:</p>
  
  <pre><code>    ArrayList&lt;MyClass&gt; tmpArrya = new ArrayList&lt;MyClass&gt;(mObjList.length);
      for (int loopIndex=0;loopIndex != mObjList.length;loopIndex++) {
          tmpArrya.add(mObjList[loopIndex]);
      }
      out.writeArray(tmpArrya.toArray());
  </code></pre>
  
  <p>When reading:</p>
  
  <pre><code>    final ArrayList&lt;MyClass&gt; tmpList = 
              in.readArrayList(com.myApp.MyClass.class.getClassLoader());
      mObjList= new MyClass[tmpList.size()];
      for (int loopIndex=0;loopIndex != tmpList.size();loopIndex++) {
          mObjList[loopIndex] = tmpList.get(loopIndex);
      }
  </code></pre>
  
  <p>But now I get a <code>NullPointerException</code>. Is above approach is correct? Why it is throwing an NPE?</p>