2012-02-28 4 views
2

아래 간단한 코드가 있습니다. 목록에서 반복 할 때 ArrayList 반복자가 null을 반환합니다. 누구나 반복자가 null을 반환하도록 만들 수있는 아이디어가 있습니까? 불행히도 이것은 실제 데이터가 없습니다. 이것은 다른 전화기에서만 발생하기 때문에 allDisplayNames에 무엇이 있는지 전혀 알지 못합니다.하지만 null이 아닌지 확인하면 목록이 초기화됩니다. 미리 감사드립니다. String[] temp = string.split(" ");string가 null 수단 여기Android ArrayList 반복자가 null을 반환합니다.

  HashMap<Integer, List<String>> allDisplayNames = Contact 
        .getAllFullnames(); 
      ... 
      for (Contact contact : Application.getContacts().values()) { 
       ... 
       ArrayList<String> namechunks = new ArrayList<String>(); 
       List<String> displaynames = new ArrayList<String>(); 
       displaynames.add(contact.getDisplay_name()); 
       if (allDisplayNames.get(contact.getId()) != null) { 
        displaynames.addAll(allDisplayNames.get(contact.getId())); 
       } 
       for (String string : displaynames) { 
        if (string==null) { 
         continue; 
        } 
        String[] temp = string.split(" "); <-- this line used to trigger NullPointer 
        ... 
       } 
+0

contact.getDisplay_name을 변경하지 마십시오입니다()는 null을 반환합니다. –

+0

고맙습니다. 나는 그것을 시험해 보았고 arraylist에 null을 추가 할 수 있다고 밝혀졌습니다 ... 문서는 "null을 포함한 모든 요소를 ​​허용합니다"라고 말합니다. – stoilkov

답변

0

는 예외이다 : 여기

코드이다. 따라서 ArrayList에는 null 값도 포함됩니다.
가능한 해결 방법 중 하나는 당신의 ArrayList에 null 값을 추가, 또는 당신의 널 (null) 검사,

if (string.equals(null)) { 
     continue; 
    } 
관련 문제