簡體   English   中英

錯誤:java.lang.nullpointerexception

[英]Error: java.lang.nullpointerexception

我被這個問題困擾,我不知道我的代碼出什么問題了,請幫幫我。 我在此代碼上收到error 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);
}

如果要從其他線程加載HashMap,則在使用時可能仍為null。 h也可以為null。 多一點細節會很好

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() ,您會得到異常。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM