I'm having a problem with altering the value of a mutable map. I assume it's something to do with nullable types, but I'm not sure. The code producing the error is below:
fun countHexadecimalNumbers(codes: List<String>): Map<Int, Int> {
val map = mutableMapOf<Int, Int>()
for (s in codes) {
val num = s.toIntOrNull(16)
if (num != null) {
if (num !in map.keys) {
map[num] = 0
}
map.getValue(num) += 1 %<--This line causes the issue
}
}
return map
When I try and build the code I get this error:
Nullable Types\Exercise 3\src\Task.kt: (13, 11): Variable expected
Does anyone have any idea why this isn't allowed?