Definition
- This operator converts the type of a boxed variable to not-null type.
- This is represented by ”!!”
- When the asserted variable holds a null value, Null Pointer Exception occurs.
Syntax
//"value" variable is asserted to a not null value.
val variable_name = value!!.length
Not Null Assertion Operator(!!) Example Program
//Not Null Assertion Operator Example Program in Kotlin
//Operator Kotlin Programs, Basic Kotlin Program
fun main(args: Array<String>) {
//Declaring boxed String
var name: String? = "Little Drops"
//Printing the length of the asserted value
println("Length of $name is ${name!!.length}")
//Setting null to the name variable
name = null
//This statement will create Null Pointer Exception
println("Length of $name is ${name!!.length}")
}
Sample Output
Length of Little Drops is 12
Exception in thread "main" kotlin.KotlinNullPointerException
at NotNullAssertionOperatorKt.main(NotNullAssertionOperator.kt:20)