The following source code demonstrates the problem:
Bar.java
@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Bar { @XmlElement @XmlJavaTypeAdapter(TestAdapter.class) private String v; public Bar() { } public Bar(String v) { this.v = v; } public static void main(String[] args) throws Exception { Marshaller m = JAXBContext.newInstance(Bar.class).createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(new Bar("foo"), System.out); } }
TestAdapter.java
import javax.xml.bind.annotation.adapters.XmlAdapter; public class TestAdapter extends XmlAdapter<String,String> { public String unmarshal(String v) throws Exception { return null; } public String marshal(String v) throws Exception { return null; } }
Running Bar will give the following:
Exception in thread "main" java.lang.NullPointerException at com.sun.xml.bind.v2.runtime.output.Encoded.setEscape (Encoded.java:107) at com.sun.xml.bind.v2.runtime.output.UTF8XmlOutput.doText (UTF8XmlOutput.java:305) at com.sun.xml.bind.v2.runtime.output.UTF8XmlOutput.text (UTF8XmlOutput.java:294) at com.sun.xml.bind.v2.runtime.output.IndentingUTF8XmlOutput.text (IndentingUTF8XmlOutput.java:152) at com.sun.xml.bind.v2.runtime.XMLSerializer.leafElement (XMLSerializer.java:319) at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$1.writeLeafElement (RuntimeBuiltinLeafInfoImpl.java:209) at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$1.writeLeafElement (RuntimeBuiltinLeafInfoImpl.java:208) at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.writeLeafElement(TransducedAccessor.java:250) at com.sun.xml.bind.v2.runtime.property.SingleElementLeafProperty.serializeBody (SingleElementLeafProperty.java:98) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.java:322) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent (XMLSerializer.java:587) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot (ClassBeanInfoImpl.java:312) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot (XMLSerializer.java:488) at com.sun.xml.bind.v2.runtime.MarshallerImpl.write (MarshallerImpl.java:317) at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal (MarshallerImpl.java:246) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal (AbstractMarshallerImpl.java:75) at com.toyota.dim.jaxb.npetest.Bar.main(Bar.java:32) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
I believe that the following is also caused by the same problem:
Exception in thread "main" java.lang.NullPointerException at com.sun.xml.bind.v2.runtime.output.SAXOutput.text(SAXOutput.java:114) at com.sun.xml.bind.v2.runtime.XMLSerializer.leafElement (XMLSerializer.java:303) at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$1.writeLeafElement (RuntimeBuiltinLeafInfoImpl.java:172) at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$1.writeLeafElement (RuntimeBuiltinLeafInfoImpl.java:171) at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccess orImpl.writeLeafElement(TransducedAccessor.java:214) at com.sun.xml.bind.v2.runtime.property.SingleElementLeafProperty.serializeBody (SingleElementLeafProperty.java:62) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.java:286) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.java:283) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.java:283) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType (XMLSerializer.java:663) at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem (ArrayElementNodeProperty.java:29) at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody (ArrayElementProperty.java:132) at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody (ArrayERProperty.java:101) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.java:286) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent (XMLSerializer.java:571) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot (ClassBeanInfoImpl.java:276) at com.sun.xml.bind.v2.runtime.property.ArrayReferenceNodeProperty.serializeListBod y(ArrayReferenceNodeProperty.java:78) at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody (ArrayERProperty.java:101) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.java:286) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType (XMLSerializer.java:663) at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody (SingleElementNodeProperty.java:113) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.java:286) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType (XMLSerializer.java:663) at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody (SingleElementNodeProperty.java:113) at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody (ElementBeanInfoImpl.java:120) at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody (ElementBeanInfoImpl.java:149) at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeBody (ElementBeanInfoImpl.java:269) at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot (ElementBeanInfoImpl.java:276) at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot (ElementBeanInfoImpl.java:35) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot (XMLSerializer.java:472) at com.sun.xml.bind.v2.runtime.MarshallerImpl.write (MarshallerImpl.java:301) at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal (MarshallerImpl.java:230) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal (AbstractMarshallerImpl.java:96) at com.toyota.ibus.common.ws.SOAPMessage.stringify(SOAPMessage.java:560) at com.toyota.ibus.common.ws.SOAPMessage12.stringify (SOAPMessage12.java:81) at com.toyota.ibus.common.ws.WebServiceInvoker.invoke (WebServiceInvoker.java:124) at com.toyota.ibus.common.ws.WebServiceInvoker.invoke (WebServiceInvoker.java:91) at com.toyota.ibus.common.ws.WebServiceInvoker.invoke (WebServiceInvoker.java:70) at au.com.toyota.taipan.supplier.orders.ws.Scratch.send(Scratch.java:102) at au.com.toyota.taipan.supplier.orders.ws.Scratch.testDatabaseOrders (Scratch.java:128) at au.com.toyota.taipan.supplier.orders.ws.Scratch.main(Scratch.java:50) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)