title: Deserialize a List<T> object with Gson?
body: <p>I want to transfer a list object via Google Gson, but I don't know how to deserialize generic types.</p>
  <p>What I tried after looking at <a href="https://stackoverflow.com/questions/2496494/library-to-encode-decode-from-json-to-java-util-map">this</a> (BalusC's answer):</p>
  <pre><code>MyClass mc = new Gson().fromJson(result, new List&lt;MyClass&gt;() {}.getClass());
  </code></pre>
  <p>but then I get an error in Eclipse saying &quot;The type new <code>List&lt;MyClass&gt;() {}</code> must implement the inherited abstract method...&quot; and if I use a quick fix I get a monster of over 20 method stubs.</p>
  <p>I am pretty sure that there is an easier solution, but I seem unable to find it!</p>
  <p>Now I have this:</p>
  <pre><code>Type listType = new TypeToken&lt;List&lt;MyClass&gt;&gt;() {}.getType();
  
  MyClass mc = new Gson().fromJson(result, listType);
  </code></pre>
  <p>However, I do get the following exception at the <code>fromJson</code> line:</p>
  <pre><code>java.lang.NullPointerException
  at org.apache.harmony.luni.lang.reflect.ListOfTypes.length(ListOfTypes.java:47)
  at org.apache.harmony.luni.lang.reflect.ImplForType.toString(ImplForType.java:83)
  at java.lang.StringBuilder.append(StringBuilder.java:203)
  at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:56)
  at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
  at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:76)
  at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:106)
  at com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:64)
  at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:49)
  at com.google.gson.Gson.fromJson(Gson.java:568)
  at com.google.gson.Gson.fromJson(Gson.java:515)
  at com.google.gson.Gson.fromJson(Gson.java:484)
  at com.google.gson.Gson.fromJson(Gson.java:434)
  </code></pre>
  <p>I <em>do</em> catch <code>JsonParseExceptions</code> and <code>result</code> is not null.</p>
  <p>I checked <code>listType</code> with the debugger and got the following:</p>
  <ul>
  <li><code>list Type</code>
  <ul>
  <li><code>args = ListOfTypes</code>
  <ul>
  <li><code>list = null</code></li>
  <li><code>resolvedTypes = Type[ 1 ]</code></li>
  </ul>
  </li>
  <li><code>loader = PathClassLoader</code></li>
  <li><code>ownerType0 = null</code></li>
  <li><code>ownerTypeRes = null</code></li>
  <li><code>rawType = Class (java.util.ArrayList)</code></li>
  <li><code>rawTypeName = &quot;java.util.ArrayList&quot;</code></li>
  </ul>
  </li>
  </ul>
  <p>So it seems the <code>getClass</code> invocation didn't work properly. Any suggestions...?</p>
  <p>I've checked on the <a href="http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Gener" rel="noreferrer">Gson User Guide</a>. It mentions a runtime exception that should happen during parsing a generic type to Json. I did it &quot;wrong&quot; (not shown above), just as in the example, but didn't get that exception at all. So I changed the serialization as in the user guide suggested. Didn't help, though.</p>
  <p>Edit:</p>
  <p>Solved, see my answer below.</p>