When accessing Map
elements by key, you should keep in mind that value might not be present. If value is not present,
null
will be returned. To make it possible, the type of returned value is nullable. In Kotlin, it’s not usually convenient to operate
with nullable types, so developers usually try to avoid them or convert them to non-nullable types. One of the possible solutions is to use
!!
(non-null assertion operator). If during the runtime the actual value applied to non-null asserrion operator was null
,
then NullPointerException
will be thrown. While in some cases it could still be legitimate to use !!
, accesing
Map
values is not one of them. Usage of a !!
when accesing Map
values is a bad practice and can lead to
NullPointerException
in Kotlin and potential crashes, if Java interop was involved.