我的代码类似于以下内容:
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))
?