Skip to main content

Unmarshal NullPointerException

4 replies [Last post]
ibsscott
Offline
Joined: 2005-07-19
Points: 0

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();
myJAXBDocument.setMyJAXB(myJAXB);
myJAXB.setMyElement("test");

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
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.setProperty("com.sun.xml.bind.xmlDeclaration",Boolean.FALSE);
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.
kohsuke
Offline
Joined: 2003-06-09
Points: 0

This forum is for discussing 2.0 things. So please consider redirecting the 1.0 questions to users@jaxb.dev.java.net.

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

See you in the users list...

ibsscott
Offline
Joined: 2005-07-19
Points: 0

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.

java.lang.NullPointerException

at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:169)

at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:92)

at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:217)

at mypackage2.Class2.test(Class2.java:43)

at mypackage2.Class2.main(Class2.java:56)

kohsuke
Offline
Joined: 2003-06-09
Points: 0

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

System.out.println(doc.getClass().getName());

?

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

ibsscott
Offline
Joined: 2005-07-19
Points: 0

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:
oracle.xml.parser.v2.XMLDocument

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.