Not-Null Assertion

The !! Operator

x!! tells the compiler "I am sure this is not null". If you are wrong, it throws NullPointerException — exactly the crash Kotlin tries to prevent.

Example

Works when the value is present:

fun main() {
    val token: String? = "abc123"
    println(token!!.length)
}

Output

6

Crash with !!

Crashes when it is null:

fun main() {
    val token: String? = null
    println("Reading token...")
    println(token!!.length)
}

Output

Reading token...
Exception: java.lang.NullPointerException

When to Use It

  • Avoid !! in normal code. Prefer ?., ?:, let or an if check.
  • It is acceptable in tests, or when a null really means a programming bug and you want to fail fast.
  • Many teams search code reviews for !! and ask for a reason.

Tip: requireNotNull(x) { "message" } is a clearer alternative: it throws with your own message.