Java XML Transformer

时间:2014-08-09 01:36:54

标签: java xml transformer

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

WriteConfig:

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);

谢谢!

2 个答案:

答案 0 :(得分:0)

在尝试将String name;用于WriteConfig之前,您从未在e.appendChild(dom.createTextNode(name));中初始化name。初始化{{1}}变量以修复错误。

答案 1 :(得分: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";