users@jaxb.java.net

RE: JAXB 2.0 Unmarshal NullPointerException

From: Scott Allan <sallan1000_at_aol.com>
Date: Thu, 21 Jul 2005 15:31:01 -0500

More information that might be helpful.
 
Here is the stack trace:
 
java.lang.NullPointerException
 at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:169)
 at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:92)
 at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unma
rshallerImpl.java:217)
 at mypackage2.Class2.test(Class2.java:43)
 at mypackage2.Class2.main(Class2.java:56)
 
 
This is the entire code (I had a typo in my last post):
 
package mypackage2;
import com.mysite.jaxb.MyJAXBDocument;
import com.mysite.jaxb.MyJAXBType;
import com.mysite.jaxb.ObjectFactory;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
 
public class Class2
{
  public Class2()
  {
  }
  public void test()
  {
  try{
    ObjectFactory objFactory = new ObjectFactory();
    MyJAXBDocument myJAXBDocument = objFactory.createMyJAXBDocument();
    MyJAXBType myJAXB = objFactory.createMyJAXBType();
    myJAXBDocument.setMyJAXB(myJAXB);
    myJAXB.setMyElement("test");
    
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();
    org.w3c.dom.Element root = doc.getDocumentElement();
    
    JAXBContext jc =
JAXBContext.newInstance(com.mysite.jaxb.ObjectFactory.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty("com.sun.xml.bind.xmlDeclaration",Boolean.FALSE);
    m.marshal(myJAXBDocument, System.out); /* this works */
    m.marshal(myJAXBDocument, doc);
    
    
    Unmarshaller u = jc.createUnmarshaller();
// Object o = u.unmarshal(root); /* this gives null pointer */
 // m.marshal(o, System.out);
    Object o = u.unmarshal(doc); /* this also does not work */
    m.marshal(o, System.out);
  }
  catch(Exception e)
  {
    e.printStackTrace();
  }
  
 
  }
  public static void main(String[] args)
  {
    Class2 class2 = new Class2();
    class2.test();
  }
}
 
Here is the schema:
 
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
           xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
           jaxb:extensionBindingPrefixes="xjc"
           jaxb:version="2.0">
 <xs:element name="myJAXBDocument">
    <xs:complexType>
       <xs:sequence>
  <xs:element name="myJAXB" type="myJAXBType" />
       </xs:sequence>
    </xs:complexType>
 </xs:element>
 
 <xs:complexType name="myJAXBType">
  <xs:sequence>
              <xs:element name="myElement" type="xs:string" />
  </xs:sequence>
 </xs:complexType>
 
 <xs:annotation>
 <xs:appinfo>
  <jaxb:schemaBindings>
   <jaxb:package name="com.mysite.jaxb"/>
  </jaxb:schemaBindings>
 
  <jaxb:globalBindings
   generateIsSetMethod="true"
   typesafeEnumBase="xs:string">
   <xjc:serializable uid="12343"/>
  </jaxb:globalBindings>
 </xs:appinfo>
 </xs:annotation>
</xs:schema>


  _____

<< ella for Spam Control >> has removed 23719 Spam messages and set
aside 2 Newsletters for me
You can use it too - and it's FREE! www.ellaforspam.com