我有一个程序需要合并两个HashMap. 哈希映射有一个键 aString和一个值 a Integer。合并的特殊条件是,如果键已经在字典中,则Integer需要将其添加到现有值中而不是替换它。这是我到目前为止抛出的代码NullPointerException。
public void addDictionary(HashMap<String, Integer> incomingDictionary) {
        for (String key : incomingDictionary.keySet()) {
            if (totalDictionary.containsKey(key)) {
                Integer newValue = incomingDictionary.get(key) + totalDictionary.get(key);
                totalDictionary.put(key, newValue);
            } else {
                totalDictionary.put(key, incomingDictionary.get(key));
            }
        }
    }