我有一个包含许多标签的XML文件。其中一些可能会丢失。因此,在使用JAXB读取这样的unmarshalled文件时,我会为那些缺少的标记获取NullPointerException。
我知道一个解决方案,我可以检查对象是否为空。但是为每个标签检查空条件并不方便,因为标签太多了。如果我处理NullPointerException它只会处理异常,但不会进一步读取xml。
有没有有效的方法来处理这种情况(特别是如何摆脱每个标签的检查null?)
Request rqstr = binfo.getRequest();
System.out.println("Requester Comment : "+rqstr.getComment());
System.out.println("Requester request Date : "+rqstr.getDate());
System.out.println("Requetser Doc Id :"+rqstr.getDocID());
System.out.println("Requetser Response date : "+rqstr.getRespondByDate());
System.out.println("Supplier check box : "+rqstr.isSupplierCheckbox());
Contact rqstrContact = rqstr.getContact();
**System.out.println("Rqstr Contact Name : "+rqstrContact.getName());**
System.out.println("Rqstr Contact title : "+rqstrContact.getTitle());
System.out.println("Rqstr : "+rqstrContact.getEmail().getAddress());
for(Phone rqstrPhone:rqstrContact.getPhone()) {
System.out.println("Requester Contact Phone : "+rqstrPhone.getNumber());
}
Company rqstrComp = rqstr.getRequestCompany();
System.out.println("Requester Company Name : "+rqstrComp.getName());
for(UniqueID rqstrUid : rqstrComp.getCompanyID()) {
System.out.println("Requester Comapny UID : "+rqstrUid.getIdentity());
}
获取NullPointerException
Exception in thread "main" java.lang.NullPointerException
at com.magnisys.eagleye.UnmarshalXml.<init>(UnmarshalXml.java:134)
at com.magnisys.eagleye.MainClass$13.widgetSelected(MainClass.java:3805)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
at com.magnisys.eagleye.MainClass.main(MainClass.java:20226)
在标有**
的行上获取NullPointerException