In the below code, my expectation is if list readerList.size = 0, headerLine should be null inside the method commonValidation(). But this is not the case, can anyone tell me why?
String[] headerLine = null;
if (readerList != null && readerList.size() != 0){
headerLine = readerList.get(0);
}
commonValidation(headerLine, logDTO, hrGroupName, feedFileName);
//method definition
public void commonValidation(String[] headerLine, TransactionLogDTO logDTO, String hrGroupName, String filename) throws PersistenceException, ServiceException, Exception {
if ((headerLine == null) || (headerLine.length == 0)){
logDTO.setGyr("R");
String subject = MessageWrapper.getMessage("hr.mail.emptyfile.subject");
String body = MessageWrapper.getMessage("hr.mail.emptyfile.body", filename);
if (logDTO.getMessage() == null){
logDTO.setMessage(body);
}
else{
logDTO.setMessage(logDTO.getMessage() + ";" + body);
}
logDTO.setIsLogErrorMailSent(Boolean.TRUE);
sendTransactionLogErrorReport(hrGroupName, subject, body);
throw new Exception(body);
}
}
String[] headerLine = null;is this a member variable? if yes then make it method local variable.Also
You can add null elements in ArrayList and size is increased.
Will Print
1not0.So check
readerList.get(0)isnullor notHere is the source code of
ArrayListaddmethodIf you see there is no check of
nullso you will have to do it yourself 🙂