在将Dom元素转换为字符串时获取NullPointerException

时间:2013-06-11 08:37:58

标签: java xml

我在以下代码中收到NullPointerException:

String str = SOME_HTML_STRING;
Node node = convertStrIntoNodeUsingJTidy();
domToString(node);

//converts node to string    
public static String domToString(org.w3c.dom.Node node)throws Exception {
    try {
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        // NullPointerException at this line, although node is not null
        transformer.transform(new DOMSource(node), new StreamResult(sw)); 
        return sw.toString();
    } catch (Exception ex) {
        throw new Exception("Error converting to String", ex);
    }
}

堆栈跟踪是:

Caused by: javax.xml.transform.TransformerException: java.lang.NullPointerException
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at com.example.Util.domToString(Util.java:3179)
... 128 more
Caused by: java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.ToXMLStream.processingInstruction(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.processingInstruction(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
... 131 more

我已经检查过以下链接: http://geekymeeky.blogspot.in/2008/10/javaxxmltransform-transformerexception.html

上面的帖子说,如果文本节点为null,则会发生此异常。

我想知道,变量 str 的值导致文本节点值为null并导致此异常。

1 个答案:

答案 0 :(得分:15)

正如您在堆栈跟踪中看到的,当解析器尝试解析一些不正确的processing instruction时,Exception被抛出。

您可以使用以下代码轻松复制它:

    Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Node n = d.createProcessingInstruction("test", null);
    System.out.println(domToString(n));

几乎像你一样引发异常。

Caused by: javax.xml.transform.TransformerException: java.lang.NullPointerException
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:732)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:336)
    at TestNode.domToString(TestNode.java:63)
    ... 31 more
Caused by: java.lang.NullPointerException
    at com.sun.org.apache.xml.internal.serializer.ToXMLStream.processingInstruction(ToXMLStream.java:281)
    at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.processingInstruction(ToUnknownStream.java:886)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:235)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:90)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:679)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:723)
    ... 33 more

修改

当您获得空注释或textNodes时会发生类似的异常,例如:

    Node n = d.createComment("test");
    n.setTextContent(null); // throws exception on parse


    Node n = d.createTextNode(null); // throws exception on parse

这种情况并不常见,只有当错误/损坏的xml作为输入源传递时才有可能。