Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding XMLAdapter causes NullPointerException in JAXBContext.newInstance method
    primarykey
    data
    text
    <p>I'm in the process of learning JAXB and wanted to try out some adapters. When I added one to my very simple class it caused the JAXBContext.newInstance() call to throw a NullPointerException. Note that the adapter isn't strictly necessary. If I comment out the @XmlJavaTypeAdapter(MapTypeAdaptor.class) annotation the code works. But that doesn't help me learn how to use adapters... Adding MapType.class and MapTypeEntry.class to JAXBContext.getInstance() didn't fix the problem either.</p> <p>I would really appreciate any advice on what I'm doing wrong. Thanks!</p> <p>Here's the Java object I'm marshaling:</p> <pre><code>import java.util.List; import java.util.Map; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.s5a.api.models.jaxb.MapTypeAdaptor; @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement public class TestCollections { @XmlJavaTypeAdapter(MapTypeAdaptor.class) // &lt;---Adding this line causes the error private Map&lt;String, Object&gt; oneColor; public Map&lt;String, Object&gt; getColor() { return oneColor; } public void setColor(Map&lt;String, Object&gt; oneColor) { this.oneColor = oneColor; } public List&lt;String&gt; getListOfColors() { return listOfColors; } public void setListOfColors(List&lt;String&gt; listOfColors) { this.listOfColors = listOfColors; } private List&lt;String&gt; listOfColors; } </code></pre> <p>Here's the adapter:</p> <pre><code>import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import javax.xml.bind.annotation.adapters.XmlAdapter; public class MapTypeAdaptor extends XmlAdapter&lt;MapType, Map&lt;String, Object&gt;&gt; { @Override public MapType marshal(Map&lt;String, Object&gt; map) throws Exception { ArrayList&lt;MapEntryType&gt; entries = new ArrayList&lt;MapEntryType&gt;(); MapType mapType = new MapType(); if (map != null &amp;&amp; map.entrySet() != null){ for (Entry&lt;String, Object&gt; entry : map.entrySet()) { MapEntryType mapEntryType = new MapEntryType(); if (entry != null){ mapEntryType.setKey(entry.getKey()); mapEntryType.setValue(entry.getValue()); } entries.add(mapEntryType); } mapType.setEntries(entries); } return mapType; } @Override public Map&lt;String, Object&gt; unmarshal(MapType map) throws Exception { HashMap&lt;String, Object&gt; hashMap = new HashMap&lt;String, Object&gt;(); if (map != null){ for (MapEntryType entryType : map.getEntries()) { if (entryType != null){ hashMap.put(entryType.getKey(), entryType.getValue()); } } } return hashMap; } } </code></pre> <p>Here's the MapType class:</p> <pre><code>import java.util.ArrayList; import java.util.List; public class MapType { private List&lt;MapEntryType&gt; mapEntries = new ArrayList&lt;MapEntryType&gt;(); public List&lt;MapEntryType&gt; getEntries() { return mapEntries; } public void setEntries(List&lt;MapEntryType&gt; mapEntries) { this.mapEntries = mapEntries; } } </code></pre> <p>And here's the MapEntryType class:</p> <pre><code>import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlValue; @XmlAccessorType(XmlAccessType.FIELD) public class MapEntryType { @XmlAttribute private String key; @XmlValue private Object value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } } </code></pre> <p>Finally, my unit test:</p> <pre><code>@Test public void shouldReturnXMLRepresentation() throws Exception { TestCollections test = new TestCollections(); HashMap&lt;String, Object&gt; color1 = new HashMap&lt;String, Object&gt;(); color1.put("blue", new Integer(50)); HashMap&lt;String, Object&gt; color2 = new HashMap&lt;String, Object&gt;(); color2.put("red", "red is the 2nd color"); ArrayList&lt;Map&lt;String, Object&gt;&gt; colors = new ArrayList&lt;Map&lt;String, Object&gt;&gt;(); colors.add(color1); colors.add(color2); test.setColor(color1); ArrayList&lt;String&gt; listofstrings = new ArrayList&lt;String&gt;(); listofstrings.add("foo"); listofstrings.add("bar"); test.setListOfColors(listofstrings); String xmlRepresentaion = genXML(test); assertTrue(xmlRepresentaion != null); assertTrue(xmlRepresentaion.length() &gt; 0); } private String genXML(Object object) throws Exception{ StringWriter writer = new StringWriter(); try { /* I tried the following, but it also throw an NPE JAXBContext jc = JAXBContext.newInstance(TestCollections.class, MapType.class, MapTypeEntry.class); */ JAXBContext jc = JAXBContext.newInstance(TestCollections.class); //&lt;-- NPE Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(object, writer); }catch (Exception e){ System.out.println("Error marshalling: " + e.getMessage()); e.printStackTrace(System.out); throw e; } System.out.println(writer.toString()); return writer.toString(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload