我一直在尝试创建 XML 文档,但不断收到 Null Pointer Exception 错误,我花了几个小时试图找出原因。
该路径是正确的,因为它在读取文件时有效。
主要的:
public static final String PATH = "res/config.xml";
public Main() {
}
public static void main(String args[]) throws FileNotFoundException {
new WriteConfig(PATH);
}
写入配置:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class WriteConfig {
Document dom;
Element e = null;
String name;
String gender;
String race;
String cclass;
public WriteConfig(String xml) {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
dom = docBuilder.newDocument();
Element mainRootElement = dom.createElement("character");
e = dom.createElement("name");
e.appendChild(dom.createTextNode(name));
mainRootElement.appendChild(e);
e = dom.createElement("gender");
e.appendChild(dom.createTextNode(gender));
mainRootElement.appendChild(e);
e = dom.createElement("race");
e.appendChild(dom.createTextNode(race));
mainRootElement.appendChild(e);
dom.appendChild(mainRootElement);
try {
Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tr.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "res/config.dtd");
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource dSource = new DOMSource(dom);
StreamResult sResult = new StreamResult(xml);
tr.transform(dSource, sResult);
} catch (TransformerException te) {
te.printStackTrace();
}
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
}
}
错误:
ERROR: ''
javax.xml.transform.TransformerException: java.lang.NullPointerException
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:752)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:357)
at bamberger.com.engine.WriteConfig.<init>(WriteConfig.java:64)
at bamberger.com.engine.Main.main(Main.java:13)
Caused by: java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.ToStream.characters(ToStream.java:1612)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:244)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:230)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:230)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:136)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:98)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:699)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:743)
... 3 more
---------
java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.ToStream.characters(ToStream.java:1612)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:244)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:230)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:230)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:136)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:98)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:699)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:743)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:357)
at bamberger.com.engine.WriteConfig.<init>(WriteConfig.java:64)
at bamberger.com.engine.Main.main(Main.java:13)
WriteConfig.java:64
tr.transform(dSource, sResult);
谢谢!