[英]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.