获取 NullPointerException,同时将 Dom 元素转换为字符串 [已关闭]

2022-09-04 20:24:10

我在以下代码中获得了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的什么值导致文本节点值为空并导致此异常。


答案 1

正如您在堆栈跟踪中看到的,当解析器尝试解析某些不正确的处理指令时,会引发异常。

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

    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被传递为输入源时才有可能。


答案 2

推荐