Skip to main content

Unmarshal NullPointerException

4 replies [Last post]
Joined: 2005-07-19

I'm trying to marshal/unmarshal an object as an org.w3c.dom.Document. Marshaling works to System.out and there is no error thrown marshaling to the Document, but I get a NullPointerException when I try to unmarshal it. Any idea what I'm doing wrong? This worked in JAXB 1.0.

ObjectFactory objFactory = new ObjectFactory();
MyJAXBDocument myJAXBDocument = objFactory.createMyJAXBDocument();
MyJAXBType myJAXB = objFactory.createMyJAXBType();

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
org.w3c.dom.Element root = doc.getDocumentElement();

JAXBContext jc = JAXBContext.newInstance(com.mysite.jaxb.ObjectFactory.class);
Marshaller m = jc.createMarshaller();
m.marshal(myJAXBDocument, System.out); /* this works */
m.marshal(myJAXBDocument, doc);

Unmarshaller u = jc.createUnmarshaller();
Object o = u.unmarshal(root); /* this gives null pointer */
m.marshal(o, System.out);
o = u.unmarshal(doc); /* this also does not work */
m.marshal(o, System.out);

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-09

This forum is for discussing 2.0 things. So please consider redirecting the 1.0 questions to

I'd like to see the stack trace of NPE.

See you in the users list...

Joined: 2005-07-19

This [b]is[/b] a JAXB 2.0 question. I'm sorry if I was not clear about that. I'm using code that has previously worked in JAXB 1.0. I thought that might be relevant. Here is the stack trace.


at com.sun.xml.bind.unmarshaller.DOMScanner.visit(

at com.sun.xml.bind.unmarshaller.DOMScanner.scan(

at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(

at mypackage2.Class2.test(

at mypackage2.Class2.main(

Joined: 2003-06-09

Sorry, you are right. I misread your post.

The code dies because the "attributes" in the following line is null:

public void visit( Element e ) throws SAXException {
NamedNodeMap attributes = e.getAttributes();

According to the javadoc of Node.getAttributes(), it is illegal to return null from this method if e is an Element (and in this case it obviously is.)

So I suspect a bug in your DOM implementation. What DOM implementation are you using? Can you check



I'll change the code in question to be more defensive. So that it can work with broken DOMs better.

Joined: 2005-07-19

Please ignore my post in the mailing list then! Sorry.

You are a genius! I was running it in JDeveloper 10.1.3 Preview. This is the DOM implementation:

I was also getting the same result when running it in JBoss 4.0.2. It turns out I was deploying the OracleSOAP package. When I took out that class, it used org.apache.xerces.dom.DocumentImpl and worked pefectly.

Thank you so much.