Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting NullPointerException while transforming Dom element to string [closed]

Tags:

java

xml

I am getting NullPointerException in the following code:

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

The stacktrace is:

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

I have already checked the following link: http://geekymeeky.blogspot.in/2008/10/javaxxmltransform-transformerexception.html

The above post says, if a text node is null then this exception occurs.

I want to know, what value of variable str caused the text node value to null and lead to this exception.

like image 369
Dheeraj Kumar Aggarwal Avatar asked Jun 11 '13 08:06

Dheeraj Kumar Aggarwal


People also ask

How do I fix NullPointerException?

NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

Why NullPointerException is coming?

The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.

How do I ignore Java Lang NullPointerException?

Answer: Some of the best practices to avoid NullPointerException are: Use equals() and equalsIgnoreCase() method with String literal instead of using it on the unknown object that can be null. Use valueOf() instead of toString() ; and both return the same result. Use Java annotation @NotNull and @Nullable.

In which case the NullPointerException will be thrown?

A null pointer exception is thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object.


1 Answers

As you can see in stack trace, Exception was throws when parser trying to parse some incorrect processing instruction.

You can easily reproduce it with code below:

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

what raises exception almost like your.

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

EDIT:

Similar exception happen when you get empty comments or textNodes, for example:

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


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

It is not common situation, and possible only if incorrect/corrupted xml was passes as input source.

like image 103
user1516873 Avatar answered Nov 05 '22 20:11

user1516873