HashMap<String, Integer> hp = new HashMap<>(); hp.put("I", 1); char[] c = s.toCharArray(); for (char sc : c ) { result += hp.get(sc); }
在使用HashMap解决LeetCode罗马数字转整数时,尝试使用了以上代码解决,结果IDEA报错:Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.HashMap.get(Object)" is null
在查看后发现,定义HashMap时使用的是<String, Integer>,而在之后的遍历中遍历的时char[ ],以此造成了报错。
需修改为?即可
HashMap<Character, Integer> hp = new HashMap<>(); hp.put('I', 1);
char[] c = s.toCharArray();
for (char sc : c ) { result += hp.get(sc); }
- NullPointerException 指针 时报 HashMapnullpointerexception指针 时报hashmap 指针springboot时报crud 指针collection hashmap方法 nullpointerexception nullpointerexception java processor internal quot nullpointerexception hashcode because nullpointerexception jfinal java lang documentationpluginsbootstrapper nullpointerexception nullpointerexception swagger quot condition patternsrequestcondition nullpointerexception