13

我在以下代码中得到 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

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

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

4

1 回答 1

17

正如您在堆栈跟踪中看到的那样,当解析器尝试解析一些不正确的处理指令时抛出异常。

您可以使用以下代码轻松重现它:

    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

编辑:

当您获得空的评论或文本节点时,也会发生类似的异常,例如:

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


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

这不是常见的情况,只有当不正确/损坏的 xml 作为输入源传递时才有可能。

于 2013-06-11T10:22:01.257 回答