title: Java generic array from null generic type
body: <p>Consider the Array.newInstance static method as a way of creating a generic type array in Java.
  What I'm failing to see how to do is create a generic array from a null generic type argument:</p>
  
  <pre><code>/**
  * Creates and fills a generic array with the given item
  */
  public static &lt;T&gt; T[] create(T item, int length)
  {
     T[] result = (T[]) Array.newInstance(item.getClass(), length);
  
     for(int i = 0; i &lt; length; i++)
        result[i] = item;
  
     return result;
  }
  </code></pre>
  
  <p>The above works when I call e.g. create("abc", 10); I'm getting a String[] of length 10 with "abc" in all positions of the array. 
  But how could I make a null string argument return a String array of length 10 and null in all positions?</p>
  
  <p>e.g.</p>
  
  <pre><code>String nullStr = null;
  String[] array = create(nullStr, 10); // boom! NullPointerException
  </code></pre>
  
  <p>Is there perhaps a  way to get the class of "item" without using one of its members (as it's null)?<br>
  I know I can just new up an array <em>String[] array = new String[10]</em>, but that's not the point.</p>
  
  <p>Thanks</p>