Search code examples
javafortify

Null Dereference - Fortify scan with HashMap.EntrySet()


I am retrieving the key and values from the Hashmap using the entrySet(). The code is working fine but the fortify scan is complaining the Null Dereference at map dot entrySet. Below is the code sample. Please suggest.

  1. I tried to put the null checks on the entry.getKey and the entry.getValue().

  2. I tried the null checks on paramMap.entrySet() as well

for (Entry<EnumType, Integer> entry : paramMap.entrySet()) {
    if (entry.getKey().name().startsWith("xyz")) {
        if (input.charAt(entry.getValue() - 1) == '1') {
            list.add(entry.getKey().name().substring(interactionPrefix.length()));
        }
    }
}


Solution

  • Instead of using the map.entrySet i used the forEach Method which internally take care of the iteration over the key and values.

    paramMap.forEach((key, value) ->{
    if (key.name().startsWith("xyz")){ 
    if (input.charAt(value - 1) == '1'){ list.add(key.name().substring(interactionPrefix.length()));
    }
    });