简体   繁体   English

使用 HashMap 的 put 方法时出现 NullPointerException

[英]NullPointerException while using put method of HashMap

The following code is giving me a NullPointerException .下面的代码给了我一个NullPointerException The problem is on the following line:问题出在以下几行:

... 
dataMap.put(nextLine[0], nextLine[6]);

What is strange is that I have run this code without the above line and the call to nextLine[0] and nextLine[6] work exactly as expected - that is they give me back elements of a csv file.奇怪的是,我在没有上面一行的情况下运行了这段代码,并且对nextLine[0]nextLine[6]的调用完全按预期工作——也就是说,它们给了我一个 csv 文件的元素。 I declare and initialise the HashMap with the code我用代码声明并初始化了HashMap

HashMap<String, String> dataMap = null;

earlier in the method在方法的前面

  String[] nextLine;
  int counter=0;
  while (counter<40) {
    counter++;

    System.out.println(counter);
    nextLine = reader.readNext(); 
    // nextLine[] is an array of values from the line
    System.out.println(nextLine[0] + " - " + nextLine[6] +" - " + "etc...");
    dataMap.put(nextLine[0], nextLine[6]);
  }
  return dataMap;
}
HashMap<String, String> dataMap = new HashMap<String,String>();

Your dataMap variable isn't initialized at this point.您的dataMap变量此时尚未初始化。 You should be getting a compiler warning about that.您应该收到有关此的编译器警告。

Where is datamap initialised ?数据映射在哪里初始化? It's always null.它始终为空。

To clarify, you declare the variable and set it to null.为了澄清起见,您声明了变量并将其设置为 null。 But you need to instantiate a new Map, whether it's a HashMap or similar.但是你需要实例化一个新的 Map,无论是 HashMap 还是类似的。

eg例如

datamap = new HashMap();

(leaving aside generics etc.) (抛开泛型等)

dataMap is declared but not initialized. dataMap 已声明但未初始化。 It can be initialized with它可以初始化为

datamap = new HashMap();数据映射 = 新 HashMap();

Well, there are three objects accessed on that line.那么,在该行上访问了三个对象。 If nextLine[0] and nextLine[6] aren't null, because the println call above worked, then that leaves dataMap.如果 nextLine[0] 和 nextLine[6] 不为 null,因为上面的 println 调用有效,那么就剩下 dataMap。 Did you do dataMap = new HashMap();你做了 dataMap = new HashMap(); somwehere?某处?

My case was different as Usually, hashmaps throw the null pointer exception when trying to read a key that is not there eg I have a我的情况与通常情况不同,当尝试读取不存在的键时,哈希图会抛出空指针异常,例如我有一个

HashMap<String, Integer> map = new HashMap<String, Integer>();

Which is empty or have keys except "someKey", So when I try to哪个是空的或有除“someKey”之外的键,所以当我尝试

map.get("someKey") == 0;

Now this will produce a nullPointerExeption due to matching a null with some integer.现在,由于将 null 与某个整数匹配,这将产生 nullPointerExeption。

What should I do instead?我应该怎么做?

Ans: I should instead check for null like Ans:我应该检查 null like

map.get("someKey") != null;

Now the runtime error Nullpointer would not raise!现在运行时错误 Nullpointer 不会引发!

Um, what exactly do you expect when you do this?嗯,能指望什么,当你做到这一点?

HashMap<String, String> dataMap = null;
...
dataMap.put(...)

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

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