Java HashMap class 抛出 NullPointerException
Java HashMap class throws a NullPointerException
我正在使用 BlueJ 并测试 HashMap class 以查看其工作原理。下面是我用来测试 class 的代码。第一次尝试调用构造函数中的 fillMyMap()
方法时,第 23 行抛出一个错误。
我尝试删除构造函数中对 fillMyMap()
的调用。 HashMapTester 对象已实例化,但当我显式调用该方法时抛出相同的 NullPointerException
。
我尝试重写 myMap
变量声明,但使用不同的语法导致编译失败。
我测试了其他 HashMap
代码(例如,来自 Objects First with BlueJ),该代码运行良好,因此不存在库、class 或包问题。
我尝试更改变量,以为我不小心碰到了保留字。同样的结果。这段代码有什么问题?
import java.util.HashMap;
public class HashMapTester
{
//Fields
public HashMap<String, String> myMap;
// The constructor is supposed to construct a new
// HashMap object with variable name myMap.
// The fillMyMap() method call simply fills the HashMap
// with data prior to testing it.
public HashMapTester()
{
HashMap<String, String> myMap = new HashMap<String, String>();
fillMyMap();
}
// fillMyMap() methods is supposed to fill up
// the keys and values of the HashMap<String, String>
// object.
public void fillMyMap()
{
myMap.put("doe", "A deer...a female deer."); //<-- ERROR OCCURS HERE!
myMap.put("ray", "A drop of golden sun.");
myMap.put("me", "A name I call myself.");
myMap.put("fah", "A long, long way to run.");
myMap.put("sew", "A needle sewing thread.");
myMap.put("la", "A note to follow sew.");
myMap.put("tea", "It goes with jam and bread.");
}
public String sing(String note)
{
String song = myMap.get(note);
return song;
}
}
您正在构造函数中创建局部变量。您没有初始化实例变量。
HashMap<String, String> myMap = new HashMap<String, String>();
是在构造函数中声明局部变量,而不是实例化字段变量。
使用
this.myMap = new HashMap<String, String>();
因为你写了HashMap<String,String> myMap=new HashMap<String,String>();
(强调第一位)。您在这里声明了一个新的局部变量。
HashMapTester()
函数应该是:
public HashMapTester(){
this.myMap=new HashMap<String,String>();
fillMyMap();
}
在您的构造函数中,您正在使用
HashMap<String,String> myMap=new HashMap<String,String>();
什么时候应该使用
this.myMap = new HashMap<String, String>();
这是实例化字段变量的方式,而不仅仅是声明一个新的局部变量映射。
最短、最简单的修复。擦除这个:HashMap<String, String>
import java.util.HashMap;
public class HashMapTester {
public HashMap<String, String> myMap;
public HashMapTester(){
myMap = new HashMap<String, String>(); //<-- I WAS THE ERROR
fillMyMap();
}
public void fillMyMap() {
myMap.put("doe", "A deer...a female deer."); //<--NO ERROR OCCURS HERE!
myMap.put("ray", "A drop of golden sun.");
myMap.put("me", "A name I call myself.");
myMap.put("fah", "A long, long way to run.");
myMap.put("sew", "A needle sewing thread.");
myMap.put("la", "A note to follow sew.");
myMap.put("tea", "It goes with jam and bread.");
}
public String sing(String note){
String song = myMap.get(note);
return song;
}
}
我正在使用 BlueJ 并测试 HashMap class 以查看其工作原理。下面是我用来测试 class 的代码。第一次尝试调用构造函数中的 fillMyMap()
方法时,第 23 行抛出一个错误。
我尝试删除构造函数中对 fillMyMap()
的调用。 HashMapTester 对象已实例化,但当我显式调用该方法时抛出相同的 NullPointerException
。
我尝试重写 myMap
变量声明,但使用不同的语法导致编译失败。
我测试了其他 HashMap
代码(例如,来自 Objects First with BlueJ),该代码运行良好,因此不存在库、class 或包问题。
我尝试更改变量,以为我不小心碰到了保留字。同样的结果。这段代码有什么问题?
import java.util.HashMap;
public class HashMapTester
{
//Fields
public HashMap<String, String> myMap;
// The constructor is supposed to construct a new
// HashMap object with variable name myMap.
// The fillMyMap() method call simply fills the HashMap
// with data prior to testing it.
public HashMapTester()
{
HashMap<String, String> myMap = new HashMap<String, String>();
fillMyMap();
}
// fillMyMap() methods is supposed to fill up
// the keys and values of the HashMap<String, String>
// object.
public void fillMyMap()
{
myMap.put("doe", "A deer...a female deer."); //<-- ERROR OCCURS HERE!
myMap.put("ray", "A drop of golden sun.");
myMap.put("me", "A name I call myself.");
myMap.put("fah", "A long, long way to run.");
myMap.put("sew", "A needle sewing thread.");
myMap.put("la", "A note to follow sew.");
myMap.put("tea", "It goes with jam and bread.");
}
public String sing(String note)
{
String song = myMap.get(note);
return song;
}
}
您正在构造函数中创建局部变量。您没有初始化实例变量。
HashMap<String, String> myMap = new HashMap<String, String>();
是在构造函数中声明局部变量,而不是实例化字段变量。
使用
this.myMap = new HashMap<String, String>();
因为你写了HashMap<String,String> myMap=new HashMap<String,String>();
(强调第一位)。您在这里声明了一个新的局部变量。
HashMapTester()
函数应该是:
public HashMapTester(){
this.myMap=new HashMap<String,String>();
fillMyMap();
}
在您的构造函数中,您正在使用
HashMap<String,String> myMap=new HashMap<String,String>();
什么时候应该使用
this.myMap = new HashMap<String, String>();
这是实例化字段变量的方式,而不仅仅是声明一个新的局部变量映射。
最短、最简单的修复。擦除这个:HashMap<String, String>
import java.util.HashMap;
public class HashMapTester {
public HashMap<String, String> myMap;
public HashMapTester(){
myMap = new HashMap<String, String>(); //<-- I WAS THE ERROR
fillMyMap();
}
public void fillMyMap() {
myMap.put("doe", "A deer...a female deer."); //<--NO ERROR OCCURS HERE!
myMap.put("ray", "A drop of golden sun.");
myMap.put("me", "A name I call myself.");
myMap.put("fah", "A long, long way to run.");
myMap.put("sew", "A needle sewing thread.");
myMap.put("la", "A note to follow sew.");
myMap.put("tea", "It goes with jam and bread.");
}
public String sing(String note){
String song = myMap.get(note);
return song;
}
}