0

2つをマージする必要があるプログラムがありますHashMap。ハッシュマップには、aであるキーと。Stringである値があり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));
            }
        }
    }
4

3 に答える 3

2

おそらく、辞書の 1 つが初期化されていません。ここに1つの解決策があります:

public void addDictionary(HashMap<String, Integer> incomingDictionary) {
    if (incomingDictionary == null) {
        throw new IllegalArgumentException("incomingDictionary cannot be null.");
    }
    if (totalDictionary == null) {
        throw new IllegalArgumentException("totalDictionary cannot be null.");
        // or another solution:
        // totalDictionary = new HashMap<String, Integer>();
        // totalDictionary.putAll(incomingDictionary);
        // return;
    }

    for (Map.Entry<String, Integer> entry : incomingDictionary.entrySet()) {
        Integer oldValue = totalDictionary.get(entry.getKey());
        if (oldValue != null){
            // here entry.getValue() could be null!
            // Never put a null value in your Map, or add a test here
            Integer newValue = entry.getValue() + oldValue;
            totalDictionary.put(entry.getKey(), newValue);
        } else {
            totalDictionary.put(entry.getKey(), entry.getValue());
        }
    }
}
于 2012-05-23T18:57:45.100 に答える
2

コードがこのメソッドに到達する前に初期化されることを保証できない場合incomingDictionaryは、null チェックを行う必要があります。

public void addDictionary(HashMap<String, Integer> incomingDictionary) {
    if (incomingDictionary == null) {
        return; // or throw runtime exception
    }
    if (totalDictionary == null) {
        return;// or throw runtime exception
    }
    if (totalDictionary.isEmpty()) {
        totalDictionary.putAll(incomingDictionary);
    } else {
        for (Entry<String, Integer> incomingIter : incomingDictionary.entrySet()) {
            String incomingKey = incomingIter.getKey();
            Integer incomingValue = incomingIter.getValue();
            Integer totalValue = totalDictionary.get(incomingKey);
            // If total dictionary contains null for the incoming key it is
            // as good as replacing it with incoming value.
            Integer sum = (totalValue == null ? 
                                            incomingValue : incomingValue == null ? 
                                                    totalValue : totalValue + incomingValue
                          );
            totalDictionary.put(incomingKey, sum);
        }
    }
}

HashMap が null を値として許可することを考慮すると、NPE が発生しやすいコード内の別の場所は

Integer newValue = incomingDictionary.get(key) + totalDictionary.get(key);

これら 2 つのいずれかが null の場合、NPE が発生します。

于 2012-05-23T18:46:43.150 に答える
0

totalDictionary正しく初期化されていることを考慮すると、次のようになります。

Integer newValue = incomingDictionary.get(key) + totalDictionary.get(key);

totalDictionary.get(key)返品できませんでしたnull
たぶん、前に次のようなものを追加する必要があります。

if(totalDictionary.get(key) == null)
  totalDictionary.put(key, 0);
于 2012-05-23T18:45:55.907 に答える