title: Gson RuntimeTypeAdapterFactory - null item to serialize leads to null pointer exception
body: <p>I am testing RuntimeTypeAdapterFactoryTest:</p>
  
  <p><a href="https://code.google.com/p/google-gson/source/browse/trunk/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java" rel="nofollow">https://code.google.com/p/google-gson/source/browse/trunk/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java</a></p>
  
  <p>which works well in the original example test case (testRuntimeTypeAdapter()):</p>
  
  <p><a href="https://code.google.com/p/google-gson/source/browse/trunk/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java#27" rel="nofollow">https://code.google.com/p/google-gson/source/browse/trunk/extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java#27</a></p>
  
  <p>But if a registered type is null, I get an NPE exception in RuntimeTypeAdapterFactory. Extending the original example above:</p>
  
  <pre><code>static class Wallet {
      BillingInstrument payment;
  }
  
  Wallet wallet = new Wallet();
  // wallet.payment = new Card("Jo", 123); // leave wallet.payment uninitialized.
  
  gson.toJson(wallet); // throws NPE
  </code></pre>
  
  <p>If I initialize wallet.payment, then serialization works fine. Here's the stack trace:</p>
  
  <pre><code>Exception in thread, java.lang.NullPointerException
  at com.me.test.RuntimeTypeAdapterFactory$1.write(RuntimeTypeAdapterFactory.java:218)
  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:91)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:206)
  at com.google.gson.Gson.toJson(Gson.java:595)
  ...
  </code></pre>
  
  <p>Which points here:</p>
  
  <p><a href="https://code.google.com/p/google-gson/source/browse/trunk/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java#218" rel="nofollow">https://code.google.com/p/google-gson/source/browse/trunk/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java#218</a></p>
  
  <p>Has anyone run into this and found a workaround? Gson is supposed to ignore serializing null values by default, so not sure why it's attempting to serialize wallet.payment in my example.</p>
  
  <p>Thanks</p>