2011-06-13 134 views
1

我正在嘗試以下內容來更新HashMap以及學生的新值,請檢查我錯過了什麼地方。訪問相同ArrayList的HashMap

ArrayList<Student> tempStudentList = XMLParser.studentHashMap.get(currentSectionName); 

if(studentList==null) 
{ 

    Log.v(CURRENT_SCREEN,"created another student list for section name:"+currentSectionName); 
    tempStudentList = new ArrayList<Student>(); 

} 
Log.v(CURRENT_SCREEN,"Added student to the list"); 

tempStudentList.add(currentStudent); 

XMLParser.studentHashMap.put(currentSectionName, tempStudentList); 
+0

什麼問題,你所看到的:此外,Java集合是可變的,在studentHashMap陣列直接,所以你不需要你的地圖上的每個查詢後執行另一個放操縱? – seanhodges 2011-06-13 12:37:15

+0

您沒有給我們足夠的上下文 - 例如,我們不知道發生了什麼問題,或者「學生」來自哪裏。 – 2011-06-13 12:38:01

回答

3

我想你想寫tempStudentList而不是studentList在空檢查。

1

您正在檢查null的錯誤變量。

ArrayList<Student> tempStudentList = XMLParser.studentHashMap.get(currentSectionName); 

if(tempStudentList == null) 
{ 
    Log.v(CURRENT_SCREEN, "created another student list for section name:"+currentSectionName); 
    tempStudentList = new ArrayList<Student>(); 
    XMLParser.studentHashMap.put(tempStudentList); 
} 

tempStudentList.add(currentStudent); 
Log.v(CURRENT_SCREEN,"Added student to the list");