title: How to Marshall Object to Element
body: <p>I'm trying to mashall an object to org.w3c.dom.Element, but it's not working.
  Here's my code:</p>
  
  <pre><code>public static org.w3c.dom.Element writeXMLElement(Object nf) {
      JAXBContext context;
      try {
          context = JAXBContext.newInstance(Rodo.class);
          Marshaller marshaller = context.createMarshaller();
          org.w3c.dom.Element retorno = new org.w3c.dom.Element() {
  
              ...
          };
          marshaller.marshal(nf, retorno);
          return retorno;
      } catch (JAXBException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return null;
  }
  </code></pre>
  
  <p>If I write XML file, it works fine. But to Element I get this error: </p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.NullPointerException
        at com.sun.xml.internal.bind.marshaller.SAX2DOMEx.startElement(Unknown Source)
        at com.sun.xml.internal.bind.v2.runtime.output.SAXOutput.endStartTag(Unknown Source)
        at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.endAttributes(Unknown Source)
        at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(Unknown Source)
        at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(Unknown Source)
        at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source)
        at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)
        at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)
        at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
        at XMLWriter.writeXMLElement(XMLWriter.java:388)
        at HTMLCTeParse.getCTeBean(HTMLCTeParse.java:339)
        at GeraCTe.main(GeraCTe.java:12)</p>
  </blockquote>
  
  <p>Does anyone know how to solve it?
  Thank you! Br</p>