简体   繁体   English

错误:java.lang.nullpointerexception

[英]Error: java.lang.nullpointerexception

I'm stucked with this problem, I don't know what's wrong with my code, please help me. 我被这个问题困扰,我不知道我的代码出什么问题了,请帮帮我。 I got error java.lang.nullpointerexception on this code: 我在此代码上收到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);
}

if you are loading HashMap from other thread it might be still null at the point of using it. 如果要从其他线程加载HashMap,则在使用时可能仍为null。 Also h may be null too. h也可以为null。 A little bit more detail will be good 多一点细节会很好

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

From that snippet, the only lines you'd be getting NullPointerException is in the hashMap.get() and hashMap.put() calls. 从该代码段中,您唯一获得NullPointerException是在hashMap.get()hashMap.put()调用中。 My bet is that hashMap was not initialized and hence is null . 我敢打赌, hashMap没有初始化,因此为null So, when you call hashMap.get() , you get your exception. 因此,当您调用hashMap.get() ,您会得到异常。

您正在使用的HashMap的实现(例如ConcurrentHashMap)可能不接受空键-如果h为空,则将导致NullPointerException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM