Consider the following code:
Example
fun main(args: Array) {
maybeWriteMessage()
}
fun maybeWriteMessage(message:
From the documentation:
The !! Operator
The third option is for NPE-lovers: the not-null assertion operator (!!
) converts any value to a non-null type and throws an exception if the value is null. We can writeb!!
, and this will return a non-null value ofb
(e.g., aString
in our example) or throw an NPE ifb
is null:
val l = b!!.length
Thus, if you want an NPE, you can have it, but you have to ask for it explicitly, and it does not appear out of the blue.
So regardless of whether your function uses the parameter or not, by using message!!
, you are explicitly asking for a NPE.