chama
chama

Reputation: 6163

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 an attribute of value. The code is below:

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

    public void setSettingValue( final Object settingValue ) {
        this.settingValue = settingValue;
    }

When I try to generate the schema (using Eclipse's 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 made this an @XmlElement instead of an attribute, the schema was generated with no issues, so it must have to do with that. Any ideas?

Upvotes: 6

Views: 9075

Answers (2)

bdoughan
bdoughan

Reputation: 149017

The NullPointerException you are seeing appears to be due to a bug in the JAXB reference implementation. You can enter a bug using the following link.

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

Workaround

You could change the property to be of type String instead. A property of type Object would not round trip anyways as unlike elements, attributes do not have any mechanisms to include typing information.


When I made this an @XmlElement instead of an attribute, the schema was generated with no issues, so it must have to do with that.

Java Model (Root)

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 can contain typing information in the form of 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>

Upvotes: 4

Puce
Puce

Reputation: 38132

Attribute types have to map to schema built-in data types or to schema simple types.

The type Object does not match these criteria.

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

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

Upvotes: 1

Related Questions