class Nest {
//我研究的龙之常数
final int DRAGON_M = 4162; // 宝藏
private int treasure;
// 居住的龙的级别
private int level;
Nest(int level) {
this.level = level;
this.treasure = level * level * DRAGON_M;
}
int getTreasure() {
return treasure;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
this.treasure = level * level * DRAGON_M;
}
}
编译并运行查看结果:
Exception in thread "main" http://www.77cn.com.cnng.NullPointerException
at Test.main(Test.java:18)
我们发现竟然报了错误,第18行出了空指针错误,也就是说get方法竟然没有拿到预期的巢穴对象。
在这里我们就要研究一下为什么取不到了。我们这里先解释一下HashMap的工作方式。