title: Getting NullPointerException while transforming Dom element to string
body: <p>I am getting NullPointerException in the following code:</p>
  
  <pre><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);
      }
  }
  </code></pre>
  
  <p>The stacktrace is:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I have already checked the following link:
  <a href="http://geekymeeky.blogspot.in/2008/10/javaxxmltransform-transformerexception.html">http://geekymeeky.blogspot.in/2008/10/javaxxmltransform-transformerexception.html</a></p>
  
  <p>The above post says, if a text node is null then this exception occurs.</p>
  
  <p>I want to know, what value of variable <strong>str</strong> caused the text node value to null and lead to this exception.</p>