I am trying to run this example
fun main() {
val mlist: MutableList<String> = mutableListOf<String>()
val mapp: MutableMap<Int, MutableList<String>> = mutableMapOf(1 to mlist)
println(mapp)
mapp[1].add("a") // Correct is mapp[1]?.add("a")
println(mapp)
}
The compiler is complaining about Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type MutableList<String>?
.
Although I have defined MutableList<String>
why the compiler says something about MutableList<String>?
.
What does the compiler understand more than me?! What am I missing here?
in mutableMap, value can be added or removed, the compiler has no guarantee that the value is still present or has been changed before accessing the value in mutableMap types