具有Object类型的JAXB属性抛出空指针异常? [英] JAXB attribute with Object type throwing null pointer exception?

查看:149
本文介绍了具有Object类型的JAXB属性抛出空指针异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图注释一个java类,用一个具有value属性的元素创建一个JAXB模式。代码如下:

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;
    }

当我尝试生成模式时(使用Eclipse的非Moxy实现),我得到这个空指针异常:

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)

当我将其设为@XmlElement而不是属性时,生成的架构没有任何问题,因此它必须与。有什么想法吗?

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?

推荐答案

你看到的 NullPointerException 似乎应该到期了到JAXB参考实现中的错误。您可以使用以下链接输入错误。

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使用 EclipseLink JAXB(MOXy) 时不会发生类似的异常作为您的JAXB提供商。

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

解决方法

您可以将属性更改为类型为 String 。类型为 Object 的属性不会进行往返,因为不同的元素,属性没有任何包含输入信息的机制。

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.


当我将其设为@XmlElement而不是属性时,生成的架构
没有任何问题,因此必须要这样做。

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模型(根)

对象是映射到XML元素时的有效属性类型。

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;
    }

}

这是因为XML元素可以包含 xsi:type 属性形式的输入信息。

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>

这篇关于具有Object类型的JAXB属性抛出空指针异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆