2012-05-22 55 views
3

當我的JAXBElement有幾個子元素時,我得到這個異常。我如何找出如何查明哪個子元素導致此異常?在JAXB編組中,如何識別哪個子元素導致JAXB編組異常?

java.lang.NullPointerException 
    at com.sun.xml.bind.v2.runtime.property.AttributeProperty.getIdValue(AttributeProperty.java:128) 
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.getId(ClassBeanInfoImpl.java:320) 
    at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor$IDREFTransducedAccessorImpl.print(TransducedAccessor.java:290) 
    at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor$IDREFTransducedAccessorImpl.print(TransducedAccessor.java:271) 
    at com.sun.xml.bind.v2.runtime.property.AttributeProperty.serializeAttributes(AttributeProperty.java:101) 
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeAttributes(ClassBeanInfoImpl.java:375) 
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:689) 
    at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158) 
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:358) 
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696) 
    at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158) 
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:358) 
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696) 
    at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:69) 
    at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:172) 
    at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:159) 
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:358) 
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696) 
    at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158) 
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:161) 
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:131) 
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:333) 
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:340) 
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:76) 
    at com.sun.xml.bind.v2.runtime.property.SingleReferenceNodeProperty.serializeBody(SingleReferenceNodeProperty.java:115) 
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:358) 
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696) 
    at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158) 
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:358) 
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696) 
    at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158) 
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:358) 
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696) 
    at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:69) 
    at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:172) 
    at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:159) 
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:358) 
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696) 
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:156) 
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:131) 
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:333) 
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:340) 
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:76) 
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494) 
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323) 
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:251) 
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75) 
    at com.tdsecurities.fxdev.doddfranktdmladapter.SwapTDMLGeneratorJunitTest.testHello(SwapTDMLGeneratorJunitTest.java:113) 

這是我用來馬歇爾Jaxbelment的代碼:

JAXBElementdmlJAXBElement = SwapDMLGenerator.generateDML(事件,leg1ordMm,leg2ordMm, leg1OrdCs,leg2OrdCs);

JAXBContext jaxbContext_m = JAXBContext.newInstance(DmlWrapper.class); 
Marshaller marshaller = jaxbContext_m.createMarshaller(); 
//marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, ""); 
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);   
String outFileName= String.format("dml-%d-ordm_deallnum-%s.xml",System.currentTimeMillis(), .getDealNumber()); 
File fout= new File(destDir, outFileName); 
FileOutputStream fos= new FileOutputStream(fout); 
marshaller.marshal(dmlJAXBElement, fos); 
fos.close(); 

回答

0

我能想到的一種方法是在項目中包含JAXB的源代碼,在調試模式下運行程序並在eclipse中放置條件轉折點。 請參閱How to set a conditional break point in eclipse 條件是該屬性爲空,因爲您正在獲得NPE。 然後,當執行在那一點停止時,您可以檢查相應的變量。

其他的替代方法是查看是否增加日誌級別可以提供更多信息。

+0

這沒有奏效。 marshaller.marshal內部發生異常,並且不輸出有缺陷的元素名稱。並且將調試級別的日誌設置增強也不會像編碼器一樣工作,編組代碼不會輸出調試日誌。 – user193116

+0

我在說的是你在調試時進入元帥方法。爲了這個工作,你需要添加到項目中的源代碼。 [示例](http://www.avajava.com/tutorials/lessons/how-do-i-view-and-debug-jdk-source-code-in-eclipse.html?page=1) – SantoshK

0

使用Eclipse IDE,可以調試一些第三方代碼,如Sun的JAXB實現。

在Eclipse中,請嘗試以下兩大塊:

啓用第三方調試支持

Search for class AttributeProperty inside your classpath entry for JRE System Library 
In Outline tab, expand class file and respective class inside this 
Right-Click on target method (in this case: AttributeProperty.getIdValue) 
Click on: Toggle Method Breakpoint 

檢查該字段是有問題的:

  1. 啓用調試Tab和變量選項卡
  2. 像其他調試過程一樣,以調試模式啓動您的測試
  3. 當程序實現相關斷點時,檢查變量選項卡'this'和'arg0'引用。這些參考資料包含有關當前節點和mashalled對象的信息

在這一點上,您已準備好上下文來分析和調試它。

我正在處理相同的JAXB/IDREF問題,但目前還沒有任何解決方案。我正在檢查它。

你解決了這個問題嗎?

最好的問候,

和過去的