Not-null assertion operator(!!) is used when you are completely certain that a variable is not null. It can be useful for example to avoid additional checks with if statement or let() function. If you have at least a little doubt use safe calls(?.).

Not-null assertion operator(!!) is also used for interoperability with the Java language. For example if some Java library returns an object, Kotlin considers it as nullable, but if you are certain and library docs says that the object can't be null, so you can use Not-null assertion operator(!!).

Answer from Sergio on Stack Overflow
🌐
Kotlin
kotlinlang.org › docs › null-safety.html
Null safety | Kotlin Documentation
Kotlin's null safety ensures safer code by catching potential null-related issues at compile time rather than runtime. This feature improves code robustness, readability, and maintainability by explicitly expressing null values, making the code ...
🌐
Baeldung
baeldung.com › home › kotlin › kotlin basics › not-null assertion (!!) operator in kotlin
Not-Null Assertion (!!) Operator in Kotlin | Baeldung on Kotlin
March 19, 2024 - However, sometimes we need to convert a nullable type to its non-nullable form forcibly. As it turns out, Kotlin’s not-null assertion operator !!
🌐
LogRocket
blog.logrocket.com › home › a complete guide to null safety in kotlin
A complete guide to null safety in Kotlin - LogRocket Blog
June 4, 2024 - Consequently, any variable cannot contain the null value unless you explicitly say it can. This way, you can avoid the danger of null references, and you do not have to wait for exceptions or errors to be thrown at runtime. Kotlin supports this possibility since its first release, and it is called null safety.
🌐
Android Developers
developer.android.com › codelabs › basic-android-kotlin-compose-nullability
Use nullability in Kotlin | Android Developers
Body 1 inside the if branch assumes that the variable is not null. Therefore, in this body, you can freely access methods or properties of the variable as if it's a non-nullable variable without using a ?. safe-call operator or a !! not-null assertion operator.
🌐
Kotlin Discussions
discuss.kotlinlang.org › t › not-null-assertion-is-pointless › 30154
Not-Null Assertion Is Pointless - Kotlin Discussions
March 26, 2025 - Hi, I’m a Java Developer who is new to Kotlin, and while I find it to be more advanced in many ways, especially syntactically, I am having trouble seeing a fair reason to use the not-null (!!) assertion operator. The way I understand it is you need (!!) only when you are absolutely certain that a variable will never be null.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › kotlin › kotlin-null-safety
Kotlin Null Safety - GeeksforGeeks
May 10, 2025 - This kind of bug has cost time and money in the software industry, which is why it's sometimes called the "billion-dollar mistake." To prevent this, Kotlin's compiler makes you clearly specify whether a variable can be null or not. If you try to use a variable that could be null without checking it, Kotlin won't let you compile the code.
🌐
Medium
medium.com › @igorwojda › kotlin-combating-non-null-assertions-5282d7b97205
Kotlin — combating non-null assertions (!!)
September 23, 2019 - I have been reviewing few Kotlin projects recently and I was greatly disturbed by the fact that developers use to much of non-null assertions (!!). This operator should be used very rarely, because it is a sign of potential NullPointerException. Each time you see !!
🌐
Simplilearn
simplilearn.com › home › resources › software development › the best article you’ll ever need to understand kotlin null safety
The Best Article You’ll Ever Need to Understand Kotlin Null Safety | Simplilearn
January 26, 2025 - In this tutorial on Kotlin Null safety, you understand various topics, including Non-nullable types and Nullable types, Causes of Null pointer exception. Click here to learn more.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
DhiWise
dhiwise.com › post › kotlin-null-safety-a-comprehensive-guide-for-developers
Mastering Kotlin Null Safety: A Comprehensive Guide
May 13, 2024 - For nullable references, Kotlin provides a few operators: Safe call operator (?): Invokes the method, if the property is not null, otherwise, returns null. ... Elvis operator (?:): Returns the left operand if it's not null; otherwise, returns ...
🌐
Suneet Agrawal
agrawalsuneet.github.io › blogs › safe-calls-vs-null-checks-in-kotlin
Safe calls(?.) vs Null checks(!!) in Kotlin · Suneet Agrawal
In any case, the nullable property requires a null check every time before accessing that property or requires a confirmation from the developer that the estimation of that property won’t be null while accessing it.
🌐
Kotlin
kotlinlang.org › api › latest › kotlin.test › kotlin.test › assert-null.html
assertNull - Kotlin Programming Language
Note that the class is not intended to be used directly from tests, use instead the top-level assertion functions which delegate to the Asserter. ... kotlin-test-junit – provides an implementation of Asserter on top of JUnit and maps the test annotations from kotlin-test-annotations-common ...
Top answer
1 of 3
13

Unfortunately, the bodies of the functions that you call, including inline functions, are not used for smart casts and nullability inference.

There's not much in your code that can be improved, and I would only suggest one thing: you can use the Elvis operator with a Nothing function for those assertion statements. The control flow analysis takes into account the branches resulting into Nothing and infers nullability from that:

fun failOnNull(): Nothing = throw AssertionError("Value should not be null")

val test: Foo? = foo()

test ?: failOnNull()
// `test` is not-null after that

This can be written without a function as well: test ?: throw AssertionError("..."), because a throw expression also has type Nothing.


Speaking of a more general case of failing an assertion, one might use a fail(...): Nothing function, which provides a bonus hint for the control flow analysis as well. The JUnit Assert.fail(...) is not a Nothing function, but you can find one in the kotlin-test-junit module or write your own one.

test as? SomeType ?: fail("`test` should be an instance of SomeType")
// smart cast works here, `test` is `SomeType`
2 of 3
8

The kotlin.test library comes with an easy solution for this:

kotlin.test.assertNotNull()

Because this function implements Kotlin contracts it supports smart casting:

contract { returns() implies (actual != null) }

Example:

    fun Foo?.assertBar() {
        assertNotNull(this)
        assertEquals(this.bar, 0)
    }

Just make sure to use the right assertNotNull import (import kotlin.test.assertNotNull)!

If you don't already use the kotlin.test library, add it to your project:

group: 'org.jetbrains.kotlin', name: 'kotlin-test', version: '1.3.11

🌐
Sling Academy
slingacademy.com › article › when-to-avoid-the-null-assertion-operator-in-kotlin
When to Avoid the Null Assertion Operator (`!!`) in Kotlin - Sling Academy
The null assertion operator is used in Kotlin to forcefully assert that a value should not be null. When you append !!
🌐
Reflectoring
reflectoring.io › kotlin-null-safety
Understanding Null Safety in Kotlin
March 1, 2024 - While Kotlin encourages null safety, ... point in your code. In such cases, you can use the not-null assertion operator to tell the compiler that you are taking responsibility for ......
🌐
Sling Academy
slingacademy.com › article › using-null-assertions-in-kotlin
Using Null Assertions (`!!`) in Kotlin - Sling Academy
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.
🌐
GitHub
github.com › detekt › detekt › issues › 4832
Disallow not null assertion (!!) · Issue #4832 · detekt/detekt
March 31, 2022 - var someVar: SomeClass? = null if (someVar != null) { someVar!!.doSomething() // !!
Published   May 12, 2022