开发者

Transformer's transform causes a fatal error, why?

I've built a document using JAXP like this:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element rootElement = document.createElement("Root");

for (MyObject o : myCollection) {
    Element entry = document.createElement("Entry");
    Element entryItem = document.createElement("EntryItem");
    entryItem.appendChild(document.createTextNode(o.getProperty()));

    entry.appendChild(entryItem);
    rootElement.appendChild(entry);
}

document.appendChild(rootElement);

Now, when I try to output the XML for the document like this:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new S开发者_运维知识库treamResult(new StringWriter());
transformer.transform(source, result);
System.out.println(result.getWriter().toString());

It falls apart on the transformer.transform line with the following error:

FATAL ERROR:  'java.lang.NullPointerException'
       :null

How do I go about debugging this? I've made sure that transformer, source and result aren't null.


I'm guessing that this:

entryItem.appendChild(document.createTextNode(o.getProperty()));

created a text node with a null value. Looking at Xerces' code (which is the default JAXP implementation shipped with Oracle's JDK 1.6), I see no null validation done at the time of constructing the text node. I suspect that that, later, makes the Transformer die.

Either that, or there's some JAXp configuration problem.

You may wish to set the jaxp.debug system property (available JDK 1.6+) to get some JAXP tracing information.


--How about the document?

Ooops sorry, obviously the second part follows the first :) Which parser are you using?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜