title: JAXB Marshall a Map structure of generic type
body: <p>Following this example (http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html) of Blaise Doughan that works, i am trying to generalize it to any type using generics.
  I obtained : </p>
  
  <pre><code>import java.util.*;
  import javax.xml.bind.annotation.*;
  
  @XmlRootElement
  @XmlAccessorType(XmlAccessType.FIELD)
  public class MyMapType&lt;K, V&gt; {
  
      public List&lt;MyMapEntryType&lt;K, V&gt;&gt; entry;
  
      public MyMapType() {
          entry = new ArrayList&lt;MyMapEntryType&lt;K, V&gt;&gt;();
      }
  
  }
  </code></pre>
  
  <p>and ,</p>
  
  <pre><code>import javax.xml.bind.annotation.*;
  
  public class MyMapEntryType&lt;K, V&gt; {
  
      @XmlAttribute
      public K key;
  
      @XmlValue
      public V value;
  
  }
  </code></pre>
  
  <p>then,</p>
  
  <pre><code>import java.util.*;
  import java.util.Map.Entry;
  
  import javax.xml.bind.annotation.adapters.XmlAdapter;
  
  public final class MyMapAdapter&lt;K, V&gt; extends
          XmlAdapter&lt;MyMapType&lt;K, V&gt;, Map&lt;K, V&gt;&gt; {
  
      @Override
      public MyMapType&lt;K, V&gt; marshal(Map&lt;K, V&gt; arg0) throws Exception {
          MyMapType&lt;K, V&gt; myMapType = new MyMapType&lt;K, V&gt;();
          for (Entry&lt;K, V&gt; entry : arg0.entrySet()) {
              MyMapEntryType&lt;K, V&gt; myMapEntryType = new MyMapEntryType&lt;K, V&gt;();
              myMapEntryType.key = entry.getKey();
              myMapEntryType.value = entry.getValue();
              myMapType.entry.add(myMapEntryType);
          }
          return myMapType;
      }
  
      @Override
      public Map&lt;K, V&gt; unmarshal(MyMapType&lt;K, V&gt; arg0) throws Exception {
          HashMap&lt;K, V&gt; hashMap = new HashMap&lt;K, V&gt;();
          for (MyMapEntryType&lt;K, V&gt; myEntryType : arg0.entry) {
              hashMap.put(myEntryType.key, myEntryType.value);
          }
          return hashMap;
      }
  
  }
  </code></pre>
  
  <p>finally,</p>
  
  <pre><code>package forum832656;
  
  import java.util.*;
  import javax.xml.bind.annotation.*;
  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  
  @XmlRootElement
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Foo&lt;K, V&gt; {
  
      @XmlJavaTypeAdapter(MyMapAdapter.class)
      Map&lt;K, V&gt; map = new HashMap&lt;K, V&gt;();
  
      public Foo() {
      }
  
      public Map&lt;K, V&gt; getMap() {
          return map;
      }
  
      public void setMap(Map&lt;K, V&gt; map) {
          this.map = map;
      }
  
  }
  </code></pre>
  
  <p>Then i tried this,</p>
  
  <pre><code> Foo&lt;Integer,String&gt; f = new Foo&lt;Integer,String&gt;();
      f.getMap().put(1, "HELLO");
      f.getMap().put(2, "WORLD");
  
  
      JAXBContext context =   JAXBContext.newInstance(Foo.class,MyMapType.class);
          Marshaller m = context.createMarshaller();
          m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
          m.marshal(f, System.out);
  </code></pre>
  
  <p>But i obtained a NullPointerException on JAXBContext creation line....
  Any idea?</p>