• Post Reply Bookmark Topic Watch Topic
  • New Topic

Null Pointer Check for multiple Fileds in java file which are obtained from XML Response

 
Arjun Majumdar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am developing an application, that gets the response from back end (DTO) as an XML file. The problem here is I map the files from the XML response to a java file in the following way:

XML PART:
<!--
some other response values for success and error codes
-->
<offerList>
<promoId>1</promoId>
<promoType>CA</promoType>
<userfiled1>userfiled1</userfiled1>
<userfiled2>3</userfiled2><!-- tenor in month type xsd:string -–>
<userfiled3>100</userfiled3> <!-- amount of type xsd:decimal -–>
<userfiled4>1</userfiled4> <!-- interest of type xsd:decimal -–>
<userfiled5>6</userfiled5>//tenor
<userfiled6>200</userfiled6>//amount
<userfiled7>2</userfiled7>//interest
<userfiled8>9</userfiled8>//tenor
<userfiled9>300</userfiled9>//amount
<userfiled10>3</userfiled10>//interest
<userfiled11>12</userfiled11>//tenor
<userfiled12>400</userfiled12>//amount
<userfiled13>4</userfiled13>//interest
<userfiled14>15</userfiled14>//tenor
<userfiled15>500</userfiled15>//amount
<userfiled16>5</userfiled16>//interest
<userfiled17>18</userfiled17>//tenor
<userfiled18>600</userfiled18>//amount
<userfiled19>6</userfiled19>//interest
<userfiled20>userfiled1</userfiled20>
<userfiled21>Offer1</userfiled21>
<cardNumber>12341234</cardNumber>

</offerList>

for(i=0;i< GetOfferLisr.size(); i++){
if(StringUtil.equals(“Offer1”, promoType)){
tenorItemList(0).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield3()).doubleValue();
tenorItemList(1).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield6()).doubleValue();
tenorItemList(2).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield9()).doubleValue();
tenorItemList(3).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield12()).doubleValue();
tenorItemList(4).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield15()).doubleValue();
tenorItemList(5).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield18()).doubleValue();


tenorItemList(0).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield4()).doubleValue();
tenorItemList(1).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield7()).doubleValue();
tenorItemList(2).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield10()).doubleValue();
tenorItemList(3).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield13()).doubleValue();
tenorItemList(4).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield16()).doubleValue();
tenorItemList(5).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield19()).doubleValue();
}
else if(StringUtil.equals(“Offer2”, promoType)){
tenorItemList(0).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield3()).doubleValue();
tenorItemList(1).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield6()).doubleValue();
tenorItemList(2).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield9()).doubleValue();
tenorItemList(3).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield12()).doubleValue();
tenorItemList(4).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield15()).doubleValue();
tenorItemList(5).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield18()).doubleValue();


tenorItemList(0).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield4()).doubleValue();
tenorItemList(1).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield7()).doubleValue();
tenorItemList(2).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield10()).doubleValue();
tenorItemList(3).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield13()).doubleValue();
tenorItemList(4).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield16()).doubleValue();
tenorItemList(5).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield19()).doubleValue();
}
else if(StringUtil.equals(“Offer3”, promoType)){
tenorItemList(0).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield3()).doubleValue();
tenorItemList(1).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield6()).doubleValue();
tenorItemList(2).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield9()).doubleValue();
tenorItemList(3).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield12()).doubleValue();
tenorItemList(4).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield15()).doubleValue();
tenorItemList(5).setAmount.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield18()).doubleValue();


tenorItemList(0).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield4()).doubleValue();
tenorItemList(1).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield7()).doubleValue();
tenorItemList(2).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield10()).doubleValue();
tenorItemList(3).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield13()).doubleValue();
tenorItemList(4).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield16()).doubleValue();
tenorItemList(5).setInterest.(((GetOfferListInqResponse).getOfferList.get(i)).getuserfield19()).doubleValue();
}

}

These elements will be shown as a table in jsp file.

now I need to add null pointer check to all the above fields. I may have all the PromoTypes in one response. If I do not have any one, then I get 0 as value. This is ok. But the most problematic thing is

when the response is as follows:
<userfiled2></userfiled2><!-- null -–>
<userfiled3></userfiled3><!-- null -–>
<userfiled4></userfiled4><!-- null -–>

I get null pointer exception.

Please help me in resolving this.
 
Arjun Majumdar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a sample code and even the original code has the same sequence. I do not want to add null pointer check to each and every field. can I put null pointer check the fields in one shot. ??
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any of these:

will throw a Null pointer exception if the input XML is null.

Also look at this:


Why Do this:

6 times! Better to do:

As for the other "gets", I would suggest writing a generic helper method that you pass the getuserfieldX to,
and in this method check for null and process accordingly.


WP

 
Arjun Majumdar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks William..

Actually this part is the existing code and I need to change the code now..

Thanks for your Help,,,
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem.

I look forward to seeing some of your new and improved code.

WP
 
Arjun Majumdar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure.. Will post the updated/new code soon after I finish updating my code with your suggestions.

 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somewhat related

http://www.coderanch.com/t/580723/java/java/Design-Null-Check-null-check
 
Arjun Majumdar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to see the post.. Much informative .. Thanks Azrael Noor
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!