Getting NullPointerException calling methods on null values unmarshalled by JAXB

I have an XML file containing many tags. Out of those some can be missing. So while reading such unmarshalled file using JAXB I am getting NullPointerException for those missing tags.

I know the one solution that I can check whether object is null or not. But it's not convenient to check null condition for each and every tag because there are too many tags. If I handle NullPointerException it would just handle the exception but will not read xml further.

Is there any efficient way to handle this situation(Especially how to get rid of checking null for each and every tag ?)

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

Getting 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)

Getting NullPointerException at line marked with **

链接地址: http://www.djcxy.com/p/76154.html

上一篇: 如何在Java中将null变成false?

下一篇: 获取NullPointerException对由JAXB解组的空值调用方法