!! Non-Null Assertion
| Since: | Kotlin 1.0(2016) |
|---|
The !! operator (non-null assertion operator) in Kotlin treats a nullable value as a non-null type. If the value is null, a NullPointerException is thrown.
Syntax
val name: String? = "item_x" val length: Int = name!!.length // Treated as String? → String. // If the value is null, NullPointerException is thrown. val nullValue: String? = null val len = nullValue!!.length // Exception thrown
Syntax List
| Syntax | Description |
|---|---|
| value!! | Returns the value as a non-null type if it is not null. Throws NullPointerException if null. |
| value!!.method() | Calls a method without a null check. Throws an exception if the value is null. |
| value!!.property | Accesses a property without a null check. |
Sample Code
sample_non_null_assertion.kt
fun getUser(id: Int): String? = if (id > 0) "User-$id" else null
fun main() {
// Works normally when the value is not null.
val name: String? = "item_x"
println(name!!.length) // 6
println(name!!.uppercase()) // ITEM_X
// Example of using !! on the result of a function where smart cast is not available
val user = getUser(1)!! // When you know the result is not null
println("User: $user") // User: User-1
// Comparison with safer alternatives to !!
val nullable: String? = "Kotlin"
// Option 1: !! (throws if null)
println(nullable!!.length) // 6
// Option 2: ?.let (safe)
nullable?.let { println(it.length) } // 6
// Option 3: ?: (fallback value if null)
println(nullable?.length ?: 0) // 6
// Using !! on a null value causes an exception.
try {
val nullStr: String? = null
println(nullStr!!.length) // Exception thrown here
} catch (e: NullPointerException) {
println("Exception caught: ${e::class.simpleName}") // NullPointerException
}
}
The command looks like this:
kotlinc sample_non_null_assertion.kt -include-runtime -d sample_non_null_assertion.jar java -jar sample_non_null_assertion.jar 6 ITEM_X User: User-1 6 6 6 Exception caught: NullPointerException
Common Mistakes
sample_non_null_assertion_mistakes.kt
fun getUserName(id: Int): String? = if (id > 0) "item_x" else null
fun main() {
// using !! on a value that can be null
val name: String? = null
println(name!!.length) // NullPointerException is thrown
// chaining !! multiple times
val user: String? = getUserName(-1)
println(user!!.trim()!!.uppercase()) // NullPointerException risk at two points
// catching the wrong exception type
try {
val nullStr: String? = null
println(nullStr!!.length)
} catch (e: KotlinNullPointerException) { // Since Kotlin 1.4, NullPointerException is thrown
println("This won't be caught")
}
}
The command looks like this:
kotlinc sample_non_null_assertion_mistakes.kt -include-runtime -d sample_non_null_assertion_mistakes.jar java -jar sample_non_null_assertion_mistakes.jar Exception in thread "main" java.lang.NullPointerException
Notes
The !! operator bypasses Kotlin's null safety features, so it tends to be used only in places where the value is certain to be non-null. In production code, developers often combine it with safer alternatives such as the ?. operator, the ?: Elvis operator, or ?.let.
Warning: Using !! where a null value can occur will throw a NullPointerException. Consider using the ?. operator, the ?: Elvis operator, or ?.let instead.
For the basics of nullable types, see Nullable Types / ?. Operator. For setting default values, see ?: Elvis Operator.
If you find any errors or copyright issues, please contact us.