Skip to main content

Null Pointer Exception when creating a JAXBContext

2 replies [Last post]
sdp198
Offline
Joined: 2005-04-06

I was wondering if anyone knows what's going wrong with my attempt at unmarhalling data from an XML file. I've managed to bind the xsd files, but I don't seem to be able to create a JAXBContext object to get and unmarshaller from.

I'm using this code to create the object and attempt to read the xml:
try {
JAXBContext jc = JAXBContext.newInstance(pcfxml.ObjectFactory.class);
Unmarshaller u = jc.createUnmarshaller();
u.unmarshal(new FileInputStream("test.xml"));
}
catch (Exception e) {
e.printStackTrace();
}

I've also tried changing the JAXBContext.newInstance() call to use:
ObjectFactory.class
and "pcfxml"
as parameters, but none of these work, I always get a NullPointerException with the stack trace below.

Can anyone suggest how I can avoid this?

Thanks,

Stephen

StackTrace:
java.lang.NullPointerException
at com.sun.xml.bind.v2.runtime.JaxBeanInfoImpl.createTypeUnmarshaller(JaxBeanInfoImpl.java:114)
at com.sun.xml.bind.v2.runtime.JaxBeanInfoImpl.(JaxBeanInfoImpl.java:91)
at com.sun.xml.bind.v2.runtime.GrammarInfo.getOrCreate(GrammarInfo.java:153)
at com.sun.xml.bind.v2.runtime.GrammarInfo.getOrCreate(GrammarInfo.java:175)
at com.sun.xml.bind.v2.runtime.AbstractElementBeanInfoImpl.(AbstractElementBeanInfoImpl.java:30)
at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.(ElementBeanInfoImpl.java:30)
at com.sun.xml.bind.v2.runtime.GrammarInfo.getOrCreate(GrammarInfo.java:138)
at com.sun.xml.bind.v2.runtime.GrammarInfo.getOrCreate(GrammarInfo.java:173)
at com.sun.xml.bind.v2.runtime.property.ArrayReferenceNodeProperty.(ArrayReferenceNodeProperty.java:33)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:72)
at com.sun.xml.bind.v2.runtime.JaxBeanInfoImpl.(JaxBeanInfoImpl.java:78)
at com.sun.xml.bind.v2.runtime.GrammarInfo.getOrCreate(GrammarInfo.java:153)
at com.sun.xml.bind.v2.runtime.GrammarInfo.(GrammarInfo.java:94)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:51)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:136)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:273)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:276)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:235)
at test.main(test.java:15)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kohsuke
Offline
Joined: 2003-06-09

Please try the new EA version, if possible. If it still doesn't fix the problem, please file an issue at https://jaxb.dev.java.net/servlets/ProjectIssues possibly with the schema you used to compile it.

sdp198
Offline
Joined: 2005-04-06

Thanks. I've now tried that, but I can't bind the schema - it seems to go into an infinite loop and ends with a stack overflow. The stack trace ends in:

at com.sun.xml.xsom.impl.ElementDecl.apply(ElementDecl.java:203)
at com.sun.tools.xjc.reader.xmlschema.ClassSelector._bindToClass(ClassSelector.java:312)
at com.sun.tools.xjc.reader.xmlschema.ClassSelector.bindToType(ClassSelector.java:256)
at com.sun.tools.xjc.reader.xmlschema.ClassSelector.isBound(ClassSelector.java:244)
at com.sun.tools.xjc.reader.xmlschema.DefaultClassBinder.complexType(DefaultClassBinder.java:93)
at com.sun.tools.xjc.reader.xmlschema.DefaultClassBinder.complexType(DefaultClassBinder.java:49)
at com.sun.tools.xjc.reader.xmlschema.ClassBinderFilter.complexType(ClassBinderFilter.java:51)
at com.sun.tools.xjc.reader.xmlschema.Abstractifier.complexType(Abstractifier.java:19)
at com.sun.tools.xjc.reader.xmlschema.Abstractifier.complexType(Abstractifier.java:13)
at com.sun.xml.xsom.impl.ComplexTypeImpl.apply(ComplexTypeImpl.java:216)
at com.sun.tools.xjc.reader.xmlschema.ClassSelector._bindToClass(ClassSelector.java:312)
at com.sun.tools.xjc.reader.xmlschema.ClassSelector.bindToType(ClassSelector.java:285)
at com.sun.tools.xjc.reader.xmlschema.DefaultClassBinder.allow(DefaultClassBinder.java:340)
at com.sun.tools.xjc.reader.xmlschema.DefaultClassBinder.elementDecl(DefaultClassBinder.java:146)
at com.sun.tools.xjc.reader.xmlschema.DefaultClassBinder.elementDecl(DefaultClassBinder.java:49)
at com.sun.tools.xjc.reader.xmlschema.ClassBinderFilter.elementDecl(ClassBinderFilter.java:91)
at com.sun.tools.xjc.reader.xmlschema.Abstractifier.elementDecl(Abstractifier.java:26)
at com.sun.tools.xjc.reader.xmlschema.Abstractifier.elementDecl(Abstractifier.java:13)
at com.sun.xml.xsom.impl.ElementDecl.apply(ElementDecl.java:203)
at com.sun.tools.xjc.reader.xmlschema.ClassSelector._bindToClass(ClassSelector.java:312)
at com.sun.tools.xjc.reader.xmlschema.ClassSelector.bindToType(ClassSelector.java:256)
at com.sun.tools.xjc.reader.xmlschema.ClassSelector.isBound(ClassSelector.java:244)
at com.sun.tools.xjc.reader.xmlschema.DefaultClassBinder.complexType(DefaultClassBinder.java:93)
at com.sun.tools.xjc.reader.xmlschema.DefaultClassBinder.complexType(DefaultClassBinder.java:49)
at com.sun.tools.xjc.reader.xmlschema.ClassBinderFilter.complexType(ClassBinderFilter.java:51)
at com.sun.tools.xjc.reader.xmlschema.Abstractifier.complexType(Abstractifier.java:19)
at com.sun.tools.xjc.reader.xmlschema.Abstractifier.complexType(Abstractifier.java:13)
at com.sun.xml.xsom.impl.ComplexTypeImpl.apply(ComplexTypeImpl.java:216)
at com.sun.tools.xjc.reader.xmlschema.ClassSelector._bindToClass(ClassSelector.java:312)
at com.sun.tools.xjc.reader.xmlschema.ClassSelector.bindToType(ClassSelector.java:285)
at com.sun.tools.xjc.reader.xmlschema.DefaultClassBinder.allow(DefaultClassBinder.java:340)
at com.sun.tools.xjc.reader.xmlschema.DefaultClassBinder.elementDecl(DefaultClassBinder.java:146)
at com.sun.tools.xjc.reader.xmlschema.DefaultClassBinder.elementDecl(DefaultClassBinder.java:49)
at com.sun.tools.xjc.reader.xmlschema.ClassBinderFilter.elementDecl(ClassBinderFilter.java:91)
at com.sun.tools.xjc.reader.xmlschema.Abstractifier.elementDecl(Abstractifier.java:26)
at com.sun.tools.xjc.reader.xmlschema.Abstractifier.elementDecl(Abstractifier.java:13)
at com.sun.xml.xsom.impl.ElementDecl.apply(ElementDecl.java:203)
at com.sun.tools.xjc.reader.xmlschema.ClassSelector._bindToClass(ClassSelector.java:312)

I'll see if I can narrow down what in the schema is causing it.