title: XML transformation with DOMResult
body: <p>I'm getting <code>java.lang.NullPointerException</code> when I uncomment a line in the following code:</p>
  
  <pre><code>private static Document XSLT(String name) throws Exception{
      File xsl = new File("data/" + name + ".xsl");
      File xml = new File("data/" + name + ".xml");
      DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document document = db.newDocument();
      DOMSource input = new DOMSource(db.parse(xml));
      StreamResult output = new StreamResult(System.out); //That works but its not what I want!
      //DOMResult output = new DOMResult(document); //that gives the null pointer exception
      TransformerFactory.newInstance().newTransformer(new StreamSource(xsl)).transform(input, output);
      return document;
  }
  </code></pre>
  
  <p>My intention is to make a method like that to help me transform a XML file, using both XSLT and Java language. After all I should do all the transformations before saving it into a file.</p>
  
  <p>How to get a Document out of that method?</p>
  
  <p>I mean, I want to do this without saving the transformed document into a file to be able to load it, I want to do it just in the "memory".</p>
  
  <p>Thank you :)</p>
  
  <h2>Update</h2>
  
  <p>Let me publish the exception so you can help me better.</p>
  
  <pre><code>Exception in thread "main" javax.xml.transform.TransformerException: java.lang.NullPointerException
          at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:736)
          at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:340)
          at test.Main.XSLT(Main.java:393)
          at test.Main.test(Main.java:342)
          at test.Main.main(Main.java:329)
  Caused by: java.lang.NullPointerException
          at com.sun.org.apache.xml.internal.serializer.SerializerBase.patchName(SerializerBase.java:271)
          at com.sun.org.apache.xml.internal.serializer.SerializerBase.addAttribute(SerializerBase.java:429)
          at com.sun.org.apache.xml.internal.serializer.ToSAXHandler.addUniqueAttribute(ToSAXHandler.java:438)
          at xmlname.template$dot$0()
          at xmlname.applyTemplates()
          at xmlname.transform()
          at com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet.transform(AbstractTranslet.java:611)
          at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:729)
          ... 4 more
  ---------
  java.lang.NullPointerException
          at com.sun.org.apache.xml.internal.serializer.SerializerBase.patchName(SerializerBase.java:271)
          at com.sun.org.apache.xml.internal.serializer.SerializerBase.addAttribute(SerializerBase.java:429)
          at com.sun.org.apache.xml.internal.serializer.ToSAXHandler.addUniqueAttribute(ToSAXHandler.java:438)
          at xmlname.template$dot$0()
          at xmlname.applyTemplates()
          at xmlname.transform()
          at com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet.transform(AbstractTranslet.java:611)
          at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:729)
          at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:340)
          at test.Main.XSLT(Main.java:393)
          at test.Main.test(Main.java:342)
          at test.Main.main(Main.java:329)
  </code></pre>
  
  <h2>Update 2</h2>
  
  <p>Lets test with a common source.</p>
  
  <p><em>test.xml</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;tests&gt;
      &lt;test&gt;
          &lt;text&gt;Hi!&lt;/text&gt;
      &lt;/test&gt;
  &lt;/tests&gt;
  </code></pre>
  
  <p><em>test.xsl</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
      &lt;xsl:output 
          method="html"
          version="1.0" 
          encoding="utf-8" 
          indent="yes" 
          omit-xml-declaration="no"
          media-type="application/xml"
          doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" 
          doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
      /&gt;
  
      &lt;xsl:template match="/"&gt;
          &lt;html xmlns="http://www.w3.org/1999/xhtml" lang="pt-BR" xml:lang="pt-BR"&gt;           
              &lt;head&gt;
                  &lt;meta charset="UTF-8" /&gt;
                  &lt;title&gt;Test&lt;/title&gt;
              &lt;/head&gt;
              &lt;body&gt;
                  &lt;h1&gt;Test&lt;/h1&gt;                       
                  &lt;xsl:apply-templates/&gt;
              &lt;/body&gt;
          &lt;/html&gt; 
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="/tests/test"&gt;
         &lt;p&gt;&lt;xsl:value-of select="text"/&gt;&lt;/p&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Gives the following output using <code>StreamResult</code>.</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;html lang="pt-BR" xml:lang="pt-BR" xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;META http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h1&gt;Test&lt;/h1&gt;
  
  &lt;p&gt;Hi!&lt;/p&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h2>Solved</h2>
  
  <p>See my answer in the appropriate place (in the answers!)</p>