我有一个定义了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指针异常。有没有明显的原因导致错误?提前谢谢。
答案 0 :(得分:1)
您收到NullPointerException
,因为mOutbox
尚未初始化
确保您在某个地方mOutbox = new AccomplishmentsOutbox();