0

I have this pojo of which there is a list is created below is shown as pojo

public class BrokerInvoiceLineItem {
    private Date dealDate;
    private String brokerRefId;
    private String receiverName;
    private double notional;
    private double fixedRate;
    private Date maturityDate;
    private double amount;

}

below is the list which is of above pojo type

 List<BrokerInvoiceLineItem> finalBrokerInvoiceLineItemList

now below is the method in which we are fetching the data and storing it to the list finally which we created above

 finalBrokerInvoiceLineItemList = brokerInvoice.getLineItems();

now in this list when I inspect while debuging , i get the value of all the attributes at particular index .

now what I am trying to say that lets say at first index the attributes are there mentioned above now lets say if any attribute value lets say if deal date is coming as null at any index then i want to throw exception or lets say brokerRefID is coming as null i want to throw an excpetion so in similiar fashion while iterating over the list at each index i want to check the value of all the 7 attributes that is dealDate, brokerRefId,receiverName,notional,fixedRate,maturityDate,amount , so in the list while iterating if any of the attributes value stored at each index is coming as null i want to throw the exception

so please advise how would I iterate finalBrokerInvoiceLineItemList and check at each index the value of the above total 7 parameters that is whether theere value is null in the finalBrokerInvoiceLineItemList abd if it null then i should throw the exception

what i have tried is shown below is

for(BrokerInvoiceLineItem item : brokerInvoice.getLineItems()) {
    if(item.getDealDate() == null)
        throw Exception();
}

but the above one is only for one attribute only not all the seven attributes

2 Answers2

0

Then you need to test all the attribute by adding || comperator in the condition as:

for(BrokerInvoiceLineItem item : brokerInvoice.getLineItems()) {

if(item.getDealDate() == null || brokerRefId == null || receiverName == null )// ... etc, add all the attribute you want here.
    throw Exception();
}
Salah
  • 8,567
  • 3
  • 26
  • 43
  • thanks a lot mate really helpful can you also please advise how can we achieve the same cross check through reflection also , Thanks in advance – gfgfh jfgjgng Nov 16 '15 at 16:18
0

You have two choices:

  1. Duplicate the if code to check all the other values (either using a logical or || or if you are throwing difference exceptions using different if statements).
  2. Use reflection to scan the class and check all values it contains.

The first option will run faster but has a lot of duplicated code.

The second option will run slower but automatically pick up and check new values added to the class.

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • thanks a lot you got my point i want to go for reflection one , request you to please advise more on the reflection part can you please post a small demo that will be a great help Thanks in advance – gfgfh jfgjgng Nov 16 '15 at 16:13
  • Try something like in this answer here: http://stackoverflow.com/a/7223577/3049628 – Tim B Nov 16 '15 at 16:17
  • i still not able to grasp completely , request you to please post a small code as a demo or take example of my class from the above question that wil help me more to grasp Thanksn a lot – gfgfh jfgjgng Nov 16 '15 at 16:29
  • The example has everything you need. Try reading up on reflection or doing some tutorials if you are still stuck. – Tim B Nov 16 '15 at 16:33