Consider the following code:
Example
fun main(args: Array) {
maybeWriteMessage()
}
fun maybeWriteMessage(message:
message: String?
is indicating that message
may or may not be null
.
Since your function maybeWriteMessage
has a default value of null
for message
and you are calling maybeWriteMessage()
without specifying message
- the default value (null
) will be used when calling writeMessage(message!!)
.
As written in the documentation the !!
-operator throws an exception when the value is null
.
One way to trigger writeMessage
safely would be to use .let
:
fun maybeWriteMessage(message: String? = null) {
message?.let {
writeMessage(it)
}
}