错误:java.lang.nullpointerexception

时间:2013-05-28 18:46:40

标签: java android 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);
}

3 个答案:

答案 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);
}

从该代码段开始,您NullPointerExceptionhashMap.get()来电中只有hashMap.put() hashMap行。我敢打赌,null未初始化,因此是hashMap.get()。因此,当您致电{{1}}时,您会收到例外情况。

答案 2 :(得分:0)

可能是你正在使用的HashMap(例如ConcurrentHashMap)的实现不接受null键 - 如果h为null,那么这将导致NullPointerException。