将不同的属性类型解组为同一个java对象时出现NullPointerException

时间:2013-02-20 15:59:40

标签: java jaxb unmarshalling


在尝试解组XML时,我遇到了严重的问题。首先,成员元素在其"值中仅包含整数"然而,现在另一个' EnumType'以字符串作为值出现 - 我得到了低于NullPointerException。

请注意,'值' Member.java中的成员来自" int"在开始时(只有Integer被接受为Member元素的值)并且它工作正常。只有当我将其更改为Object(并且我猜这是我的问题的来源)时 - 出现了以下异常。

XML:

<EnumType Name="Genre" UnderlyingType="Edm.Int32">
   <Member Name="ACTION" Value="0"/>
   <Member Name="COMEDY" Value="1"/>
</EnumType>
<EnumType Name="Rating" UnderlyingType="Edm.String">
   <Member Name="RatingA" Value="G"/>
   <Member Name="RatingB" Value="PG"/>
 </EnumType>

EnumType.java

@XmlRootElement
public class EnumType {

   @XmlElement(name = "Member", namespace = "http://schemas.microsoft.com/ado/2009/11/edm")
   private List<Member> members = new LinkedList<Member>();

   public List<Member> getMembers() {
      return members;
   }
}

Member.Java

@XmlRootElement
public class Member {

    @XmlAttribute(name = "Name")
    private String name;

    @XmlAttribute(name = "Value")
    private Object value;

    public String getName() {
        return name;
    }

    public Object getValue() {
        return value;
    }
}

当解组上述XML时,我得到了这个NullPointerException:

  

显示java.lang.NullPointerException       at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor.get(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.property.AttributeProperty。(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty。(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty。(Unknown Source)       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)       at java.lang.reflect.Constructor.newInstance(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty。(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty。(Unknown Source)       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)       at java.lang.reflect.Constructor.newInstance(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty。(Unknown Source)       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)       at java.lang.reflect.Constructor.newInstance(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty。(Unknown Source)       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)       at java.lang.reflect.Constructor.newInstance(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl。(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl。(Unknown Source)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl $ JAXBContextBuilder.build(Unknown Source)       at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)       at java.lang.reflect.Method.invoke(Unknown Source)       在javax.xml.bind.ContextFinder.newInstance(未知来源)       在javax.xml.bind.ContextFinder.newInstance(未知来源)       在javax.xml.bind.ContextFinder.find(未知来源)       在javax.xml.bind.JAXBContext.newInstance(未知来源)       在javax.xml.bind.JAXBContext.newInstance(未知来源)       在com.sap.ndb.studio.rdl.csdlparser.jaxb.JAXBParser.load(JAXBParser.java:26)       在com.sap.ndb.studio.rdl.datapreview.functions.LoadGRDL.function(LoadGRDL.java:59)       在org.eclipse.swt.browser.WebSite.Invoke(WebSite.java:773)       在org.eclipse.swt.browser.WebSite $ 7.method6(WebSite.java:129)       在org.eclipse.swt.internal.ole.win32.COMObject.callback6(COMObject.java:119)       at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)       在org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2546)       在org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3756)       在org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)       在org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)       在org.eclipse.ui.internal.Workbench.access $ 4(Workbench.java:2499)       在org.eclipse.ui.internal.Workbench $ 7.run(Workbench.java:679)       在org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)       在org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)       在org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)       在org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)       在org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)       在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)       在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)       在org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)       在org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)       at java.lang.reflect.Method.invoke(Unknown Source)       在org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)       在org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)       在org.eclipse.equinox.launcher.Main.run(Main.java:1438)       在org.eclipse.equinox.launcher.Main.main(Main.java:1414)

有什么想法吗? :(

1 个答案:

答案 0 :(得分:4)

当您使用@XmlAttribute注释Object类型的字段/属性时,JAXB RI将抛出该异常。就像你在Member课程中所拥有的那样:

@XmlAttribute(name = "Value")
private Object value;

了解更多信息