2012-02-28 77 views
2

我在下面有一個簡單的代碼,其中ArrayList迭代器在我循環列表時返回null。有沒有人有一個想法可以導致迭代器返回null?不幸的是,我沒有實際的數據,因爲這隻發生在其他手機上,我不知道什麼是allDisplayNames,但我知道列表已初始化,因爲我檢查它不等於空。 在此先感謝!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。 – 2012-02-28 08:43:25

+0

謝謝你,我試了一下,結果你可以將null添加到arraylist ...文檔中說「允許所有元素,包括null」。 – stoilkov 2012-02-28 08:49:38

回答

0

這裏,例外的是在String[] temp = string.split(" ");意味着string爲空。所以ArrayList也包含空值。
可能的解決辦法是,要麼不爲空值,ArrayList中添加或更改無效支票,

if (string.equals(null)) { 
     continue; 
    }