我遇到了这个问题,我不知道我的代码有什么问题,请帮帮我。我在这段代码上得到了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);
}
答案 0 :(得分:0)
如果从其他线程加载HashMap,它在使用它时可能仍为null。 h也可能为空。更多细节将是好的
答案 1 :(得分: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()
。因此,当您致电{{1}}时,您会收到例外情况。
答案 2 :(得分:0)
可能是你正在使用的HashMap(例如ConcurrentHashMap)的实现不接受null键 - 如果h为null,那么这将导致NullPointerException。