title: NullPointerException in Avro ReflectDatumWriter
body: <p>I have a specific issue with Avro serialization of Java Objects. I have POJO's generated from xsd schemas which I am then trying to serialize using avro to place on a kafka topic. Some of the xmlElements are optional</p>
  
  <p>A test message looks like this:</p>
  
  <pre><code>@XmlRootElement(name = "message")
  public class Testmessage {
  
    @XmlElement
    public String id;
  
  
    @XmlElement
    public String name;
  
    public Testmessage(String id, String name) {
      this.id = id;
      this.name = name;
    }
  
    public Testmessage() { }
  
    @Override
    public String toString() {
      return "Message{" +
          "id='" + id + '\'' +
          ", name=" + name +
          '}';
    }
  }
  </code></pre>
  
  <p>And the method to serialize and place on the topic is:</p>
  
  <pre><code>public void sendMessage(Testmessage msg) throws Exception{
  
      DatumWriter&lt;Testmessage&gt; writer = new ReflectDatumWriter&lt;Testmessage&gt;(Testmessage.class); 
      ByteArrayOutputStream os = new ByteArrayOutputStream();
  
      Encoder encoder = EncoderFactory.get().binaryEncoder(os, null);
      writer.write(msg, encoder);
      encoder.flush();
      os.close();
      KeyedMessage&lt;String, byte[]&gt; data = new KeyedMessage&lt;String, byte[]&gt;(TOPIC_NAME, os.toByteArray());
  
      producer.send(data);
  
  }
  </code></pre>
  
  <p>When I send both fields all works as expected. If I null one of the fields or leave it out I get NPE's from the write.</p>
  
  <pre><code>java.lang.NullPointerException: in Testmessage in string null of string in field id of    Testmessage
  at org.apache.avro.reflect.ReflectDatumWriter.write(ReflectDatumWriter.java:145)
  at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:58)
  </code></pre>
  
  <p>Any ideas? or point me in the right direction</p>
  
  <p>Thanks!</p>