2013-05-28 57 views
0

我被這個問題困住了,我不知道我的代碼有什麼問題,請幫助我。我得到這個代碼error java.lang.nullpointerexception錯誤:java.lang.nullpointerexception

List<DataPoint> listPoints; 
if((listPoints = hashMap.get(h)) == null) { 
    listPoints = new ArrayList<DataPoint>(); 
    DataPoint point = new DataPoint((int)songId, i); 
    listPoints.add(point); 
    hashMap.put(h, listPoints); 
} 
+3

後logcat所以知道哪一行是'NPE'。 'hashMap'很可能是'null' – codeMagic

+0

也許'hashMap'沒有初始化? – MrSmith42

+2

你可以發佈其餘的代碼和什麼行實際上是拋出NullPointer? – draksia

回答

0

如果要加載其他線程HashMap的可能是在使用它的點仍然空。 h也可能爲零。多一點點的細節將是很好的

0
if((listPoints = hashMap.get(h)) == null) 
{ 
    listPoints = new ArrayList<DataPoint>(); 
    DataPoint point = new DataPoint((int)songId, i); 
    listPoints.add(point); 
    hashMap.put(h, listPoints); 
} 

從這個片段中,你會得到NullPointerException唯一的線是在hashMap.get()hashMap.put()電話。我敢打賭,hashMap未初始化,因此是null。所以,當你撥打hashMap.get()時,你會得到你的例外。

0

您可能正在使用的HashMap(例如ConcurrentHashMap)的實現不接受空鍵 - 如果h爲null,則會導致NullPointerException。

相關問題