0

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);
    }
}
3
  • have you checked that the readerList.size()? isn't 0 and readerList isn't null? Otherwise I don't see a problem. Commented Sep 4, 2012 at 9:22
  • There is a logical error in this question. From accepted answer it becomes obvious that OP was not interested in "if A then B" violation, but in violation of "if B then A". Commented Sep 4, 2012 at 13:19
  • Sorry guys ,actually there was a nullpointer before the method "commonValidation" was called,which resulted in the above scenario.Seeing the answer below i actuall changed my implementation a bit in using existing value from arraylist rather than use a seperate array to capture the data from the list and then use it for validation.So i accepted the below answer.Sorry if i was wrong in accepting the answer. Commented Sep 4, 2012 at 15:25

2 Answers 2

2

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.

    ArrayList<String[]> arrayList = new ArrayList<String[]>();
    arrayList.add(null);
    System.out.println(arrayList.size());

Will Print 1 not 0.

So check readerList.get(0) is null or not

Here is the source code of ArrayList add method

 410       public boolean add(E e) {
 411           ensureCapacityInternal(size + 1);  // Increments modCount!!
 412           elementData[size++] = e;
 413           return true;
 414       }

If you see there is no check of null so you will have to do it yourself :)

Sign up to request clarification or add additional context in comments.

Comments

0

But this is not the case, (...)

I don't believe. If readerList.size() == 0 headerLine does not change the value and remains null.

1 Comment

Unless of course another thread changes the value, or other code that the OP is leaving out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.