JAXB attribute with object type throwing null pointer exception?

I am trying to annotate a java class to create a JAXB schema with an element that has a value attribute. Code below:

@XmlAttribute(name="value") public Object getSettingValue() { return this.settingValue; } public void setSettingValue( final Object settingValue ) { this.settingValue = settingValue; } 

When I try to generate a scheme (using the Eclipse non-Moxy implementation), I get this null pointer exception:

 Exception in thread "main" java.lang.NullPointerException at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:154) at com.sun.xml.internal.bind.v2.runtime.property.AttributeProperty.<init>(AttributeProperty.java:56) at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:93) at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:305) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100) at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143) at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:110) 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:597) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202) at javax.xml.bind.ContextFinder.find(ContextFinder.java:376) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522) at org.eclipse.jpt.jaxb.core.schemagen.Main.buildJaxbContext(Main.java:95) at org.eclipse.jpt.jaxb.core.schemagen.Main.generate(Main.java:76) at org.eclipse.jpt.jaxb.core.schemagen.Main.execute(Main.java:62) at org.eclipse.jpt.jaxb.core.schemagen.Main.main(Main.java:47) 

When I did this @XmlElement instead of an attribute, the schema was generated without problems, so it should be associated with this. Any ideas?

+6
source share
2 answers

NullPointerException you see is apparently caused by an error in the JAXB reference implementation. You can enter an error using the following link.

A similar exception does not occur when using EclipseLink JAXB (MOXy) as a JAXB provider.

Bypass

Instead, you can change a property of type String . A property of type Object in any case will not round, because unlike elements, attributes do not have any mechanisms to include information for input.


When I did this @XmlElement instead of an attribute, the schema was created without problems, so it should be relevant to this.

Java Model (Root)

An object is a valid property type when mapped to an XML element.

 import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Root { private Object settingValue; public Object getSettingValue() { return settingValue; } public void setSettingValue(final Object settingValue) { this.settingValue = settingValue; } } 

This is because the XML element may contain input information as an xsi:type attribute.

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <root> <settingValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">123</settingValue> </root> 
+4
source

Attribute types must be mapped to built-in schema data types or to simple schema types.

Object type does not meet these criteria.

http://www.w3schools.com/schema/el_attribute.asp

http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/annotation/XmlAttribute.html

+1
source

All Articles