2012-09-04 3 views
0

아래 코드에서 commonValidation() 메서드 내에서 readerList.size = 0, headerLinenull이어야합니다. 그러나 이것은 사실이 아닙니다. 누가 그 이유를 말해 줄 수 있습니까? 이 멤버 변수배열을 null로 초기화했으나 null 비교가 실패했습니다.

String[] headerLine = null; 

if (readerList != null && readerList.size() != 0){ 
    headerLine = readerList.get(0);   
} 

commonValidation(headerLine, logDTO, hrGroupName, feedFileName); 

// 메소드 정의

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); 
    } 
} 
+0

당신은'readerList.size()'를 확인 했습니까? 'readerList'가'null '이 아닌가? 그렇지 않으면 문제가 발생하지 않습니다. –

+0

이 질문에 논리적 인 오류가 있습니다. 수락 된 답변에서 OP가 "A B then A"위반에 관심이 없지만 "B A then B"위반에 대해서는 분명하게 알 수 있습니다. – Jarekczek

+0

죄송합니다 얘들 아, 사실 거기에 nullpointer 전에 "commonValidation"위의 scenario.See 대답 아래에 대답을 내 전화를 사용하여 기존 값을 사용하여 arraylist에서 기존의 값을 사용하여 약간의 배열을 사용하여 캡쳐 목록에서 데이터를 누른 다음 유효성 검사를 위해 그것을 사용합니다. 나는 아래의 답변을 수락했습니다. 내가 대답을 받아 들일 때 틀렸어. 죄송합니다. – ashwinsakthi

답변

2

String[] headerLine = null;입니까? 그렇다면 메소드 로컬 변수로 만듭니다.

null 요소를 ArrayList에 추가 할 수 있으며 크기가 증가합니다.

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

1하지 0를 인쇄합니다.

그래서 여기

ArrayList add 방법

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

당신은 그래서 당신은 스스로를해야 할 것 null의 어떤 검사가없는 볼 경우의 소스 코드 readerList.get(0)null인지 아닌지 확인 :)

0

그러나 이것은 사실이 아닙니다. (...)

나는 믿을 수 없다. readerList.size() == 0headerLine은 값을 변경하지 않고 null으로 유지됩니다.

+0

물론 다른 스레드가 값이나 OP가 빠져 나가는 다른 코드를 변경하지 않는 한. –

관련 문제