我已经使用 JAXP 构建了一个文档,如下所示:
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);
现在,当我尝试像这样输出文档的 XML 时:
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);
System.out.println(result.getWriter().toString());
transformer.transform
它因以下错误而分崩离析:
FATAL ERROR: 'java.lang.NullPointerException'
:null
我该如何调试呢?我已经确定了transformer
,source
并且result
不为空。