title: How to use writeStringArray() and readStringArray() in a Parcel
body: <p>I recently came across a very stupid (at least from my point of view) implementation inside Androids <code>Parcel</code> class.</p>
  
  <p>Suppose I have a simple class like this</p>
  
  <pre><code>class Foo implements Parcelable{
  
      private String[] bars;
      //other members
  
      public in describeContents(){ 
          return 0;
      }
  
      public void writeToParcel(Parcel dest, int flags){
  
          dest.writeStringArray(bars);
          //parcel others
      }
  
      private Foo(Parcel source){
          source.readStringArray(bars);
          //unparcel other members
      }
  
      public static final Parcelable.Creator&lt;Foo&gt; CREATOR = new Parcelable.Creator&lt;Foo&gt;(){
  
           public Foo createFromParcel(Parcel source){
               return new Foo(source);
           }
  
           public Foo[] newArray(int size){
               return new Foo[size];
           }
  
      };
  }
  </code></pre>
  
  <p>Now, if I want to Parcel a <code>Foo</code> Object and <code>bars</code> is null I see no way to recover from this situation (exept of catching Exceptions of course). Here is the implementation of these two methods from Parcel:</p>
  
  <pre><code> public final void writeStringArray(String[] val) {
          if (val != null) {
              int N = val.length;
              writeInt(N);
              for (int i=0; i&lt;N; i++) {
                  writeString(val[i]);
              }
          } else {
              writeInt(-1);
          }
      }
  
  
    public final void readStringArray(String[] val) {
          int N = readInt();
          if (N == val.length) {
              for (int i=0; i&lt;N; i++) {
                  val[i] = readString();
              }
          } else {
              throw new RuntimeException("bad array lengths");
          }
      }
  </code></pre>
  
  <p>So writeStringArray is fine if I pass <code>bars</code> which are null. It just writes <code>-1</code> to the Parcel. But How is the method readStringArray supposed to get used? If I pass <code>bars</code> inside (which of course is null) I will get a NullPointerException from <code>val.length</code>. If I create <code>bars</code> before like say <code>bars = new String[???]</code> I don't get any clue how big it should be. If the size doesn't match what was written inside I recieve a RuntimeException.</p>
  
  <p>Why is <code>readStringArray</code> not aware of a result of <code>-1</code> which gets written on null objects from <code>writeStringArray</code> and just returns?</p>
  
  <p>The only way I see is to save the size of <code>bars</code> before I call <code>writeStringArray(String[])</code> which makes this method kind of useless. It will also redundatly save the size of the Array twice (one time for me to remember, the second time from <code>writeStringArray</code>).</p>
  
  <p>Does anyone know how these two methods are supposed to be used, as there is NO java-doc for them on top?</p>