Java HashMap get method null pointer exception
2022-09-01 14:52:13
我的代码类似于以下内容:
public class A {
private HashMap<Character, Boolean> myMap;
public A() {
myMap = new HashMap<Character, Boolean>();
String mychars = "asdfzxcvqwer";
for (char c : mychars.toCharArray())
myMap.put(c, true);
}
public void doo(String input) {
StringBuilder output = new StringBuilder();
for (char c : input.toCharArray()) {
if (myMap.get(c))
output.append(c);
}
}
//...
//...
}
为什么我在行中出现空指针异常 (NPE)?if (myMap.get(c))