Sling Academy
Home/Kotlin/Using Null Assertions (`!!`) in Kotlin

Using Null Assertions (`!!`) in Kotlin

Last updated: November 30, 2024

When working with Kotlin, understanding how nullability works is crucial because Kotlin aims to eradicate NullPointerException from your code. However, there are certain scenarios where you consciously want to suppress nullability checks. This is where the null assertion operator (`!!`) comes into play.

What is Null Assertion (`!!`) in Kotlin?

The null assertion operator converts any value to a non-null type and throws a NullPointerException if the value is null. It's your way of saying, "I'm sure this value is non-null, and I want you to guarantee that." It’s important to use it sparingly and only when you are sure the value is indeed non-null, as misuse can reintroduce null safety issues.

Basic Usage

Let's look at a simple example of how the null assertion operator is used:


fun main() {
    val text: String? = getDefaultValue()
    // Using null assertion
    val length = text!!.length
    println("Length: $length")
}

fun getDefaultValue(): String? {
    return "Hello, Kotlin!"
}

In the above code, we use !! to assert that the variable text is not null when we attempt to access its length property. If text were to be null, it would result in a NullPointerException.

Potential Dangers

While the null assertion operator can be useful, it should be used with caution. Accidentally using it on a null value could lead to a NullPointerException, bringing you back to the issues Kotlin is designed to prevent. Let’s consider a situation where the misuse of !! might occur:


fun main() {
    val input: String? = null
    // Misuse of null assertion
    val length = input!!.length // This will throw a NullPointerException
}

Alternatives to Using Null Assertions

Before resorting to !!, consider using safe calls or the Elvis operator (?:) for a more graceful handling of null cases.

Using Safe Calls (?.)


fun main() {
    val text: String? = null
    val length: Int? = text?.length
    println("Length: $length") // Will safely print "Length: null"
}

Using the Elvis Operator


fun main() {
    val text: String? = null
    val length: Int = text?.length ?: 0
    println("Length: $length") // Will print "Length: 0"
}

By using these alternatives, you can prevent unexpected null errors and make your code more resilient and stable.

Conclusion

The null assertion operator !! can be a powerful tool in your Kotlin programming toolkit, but it should be used judiciously. Always consider safer alternatives like safe calls and the Elvis operator to handle nullability gracefully.

Next Article: When to Avoid the Null Assertion Operator (`!!`) in Kotlin

Previous Article: Combining Safe Calls and the Elvis Operator in Kotlin

Series: Null Safety in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin