调用HashMap.put时出现空指针异常

时间:2013-10-15 20:47:37

标签: java android parameters nullpointerexception hashmap

我有一个定义了HashMap的类:

class AccomplishmentsOutbox {

    HashMap<String, Boolean> mAchBools = new HashMap<String, Boolean>(){{
        put("CgkIlezvppMIEAIQAg", false);
        }
    };
}

在我的活动类中,我添加了:

AccomplishmentsOutbox mOutbox = new AccomplishmentsOutbox();

然后,在一个方法中,我尝试将HashMap布尔值设置为true:

public void onUnlockAchievement(String achievementID) {
    mOutbox.mAchBools.put(achievementID, true);
}

当我将相同的String ID(CgkIlezvppMIEAIQAg)作为参数传递时,该行抛出Null指针异常。有没有明显的原因导致错误?提前谢谢。

1 个答案:

答案 0 :(得分:1)

您收到NullPointerException,因为mOutbox尚未初始化

确保您在某个地方mOutbox = new AccomplishmentsOutbox();