尝试解组XML时遇到严重问题。首先,Member元素的“值”属性中仅包含Integer(如第一个“ EnumType”元素),但是,现在出现了另一个“ EnumType”,其中Strings作为值-我得到了下面的NullPointerException信息。
请注意,Member.java中的“值”成员在一开始就是“ int”类型(当仅接受Integer作为Member元素的值时),并且工作正常。仅当我将其更改为对象(并且我猜这是我的问题的根源)时,才会出现以下异常。

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


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
      在com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor.get(未知来源)
      位于com.sun.xml.internal.bind.v2.runtime.property.AttributeProperty。(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。处(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty。(来源未知)
      在com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty。处(未知来源)
      在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处
      在sun.reflect.NativeConstructorAccessorImpl.newInstance(未知来源)
      在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知来源)
      在java.lang.reflect.Constructor.newInstance(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。处(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty。(来源未知)
      在com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty。处(未知来源)
      在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处
      在sun.reflect.NativeConstructorAccessorImpl.newInstance(未知来源)
      在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知来源)
      在java.lang.reflect.Constructor.newInstance(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。处(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty。(来源未知)
      在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处
      在sun.reflect.NativeConstructorAccessorImpl.newInstance(未知来源)
      在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知来源)
      在java.lang.reflect.Constructor.newInstance(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。处(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty。(来源未知)
      在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处
      在sun.reflect.NativeConstructorAccessorImpl.newInstance(未知来源)
      在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知来源)
      在java.lang.reflect.Constructor.newInstance(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。处(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl。处(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl。处(未知来源)
      在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl $ JAXBContextBuilder.build(未知源)
      在com.sun.xml.internal.bind.v2.ContextFactory.createContext(未知来源)
      在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
      在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)
      在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)
      在java.lang.reflect.Method.invoke(未知来源)
      在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)
      在org.eclipse.swt.internal.win32.OS.DispatchMessageW(本机方法)
      在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)
      在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
      在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)
      在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)
      在java.lang.reflect.Method.invoke(未知来源)
      在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)


有任何想法吗? :(

最佳答案

当您用@XmlAttribute注释类型为Object的字段/属性时,JAXB RI将引发该异常。就像您在Member类中一样:

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


想要查询更多的信息


JAXB attribute with Object type throwing null pointer exception?

关于java - 将不同的属性类型解码到同一java对象时发生NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14984334/

10-13 01:15