JAXB attribute with Object type throwing null pointer exception?

假如想象 提交于 2020-02-03 05:17:47

问题


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?


回答1:


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.

  • http://java.net/jira/browse/JAXB/

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>



回答2:


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



来源:https://stackoverflow.com/questions/14899555/jaxb-attribute-with-object-type-throwing-null-pointer-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!