-1

我一直在尝试创建 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);

谢谢!

4

2 回答 2

1

String name;在尝试WriteConfig将其用于e.appendChild(dom.createTextNode(name));. 初始化name变量以修复错误。

于 2014-08-09T01:59:21.520 回答
0
String name;
String gender;
String race;
String cclass;

必须在使用它之前初始化变量。

         //Decarle variable
        String name;
        String gender;
        String race;
        String cclass;


        //Initialize and set value
        name = "EwokHugz";
        gender = "female";
        race = "10";
        cclass = "XML Programming";
于 2014-09-16T09:50:15.133 回答