Kotlin Academy · Lesson

The !! Operator

Force non-null and its risks.

Lesson 3 of 413 steps

The !! Operator is a free Kotlin Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

Forcing Non-Null

Sometimes you are certain a nullable value is not null, but the compiler still complains.

The not-null assertion operator !! tells the compiler to trust you and treat the value as non-null.

Basic Usage

Adding !! converts a nullable type to its non-null counterpart.

If the value really is non-null, the code works exactly like a normal call.

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

The Danger

If the value is actually null, !! throws a NullPointerException right there.

This is the one place Kotlin lets a NPE happen, and it is entirely your responsibility.

fun main() {
    val name: String? = null
    try {
        println(name!!.length)
    } catch (e: NullPointerException) {
        println("Boom: NullPointerException")
    }
}

Why It Exists

The operator is intentionally ugly. Two exclamation marks stand out in code review, signaling a spot where you bypassed the safety net.

It exists for the rare cases where you truly know more than the compiler.

Safe Call Is Usually Better

In most situations a safe call ?. with Elvis ?: is safer and just as short.

Compare the two approaches below for the same goal.

fun main() {
    val name: String? = null
    val safeLength = name?.length ?: 0
    println(safeLength)
}

Avoid Chaining !!

Never chain multiple !! like a!!.b!!.c. If it crashes, you cannot tell which part was null.

Break the expression apart or use safe calls instead.

fun main() {
    val text: String? = "hi"
    // bad style: text!!.uppercase()!! is hard to debug
    val good = text?.uppercase() ?: ""
    println(good)
}

A Legitimate Use

One acceptable case: a value you set earlier and know is initialized by now.

Even then, prefer lateinit or restructuring when possible.

fun main() {
    var config: String? = null
    config = "loaded"
    // we just assigned it, so !! is safe here
    println(config!!.uppercase())
}

Reading the Stack Trace

When !! fails, the exception points to the exact line. That precision is the only upside compared to a silent wrong result.

Use the message to find and fix the assumption that was wrong.

fun length(text: String?): Int {
    return text!!.length
}

fun main() {
    println(length("abc"))
    // length(null) would throw on the !! line
}

Refactoring Away !!

Most !! can be removed by handling null properly. Below, an Elvis fallback makes the function total and crash-free.

fun length(text: String?): Int {
    return text?.length ?: 0
}

fun main() {
    println(length("abc"))
    println(length(null))
}

Rule of Thumb

Treat every !! as a question: am I absolutely sure this is never null?

  • If yes and rare → acceptable
  • If unsure → use ?. and ?:

Putting It Together

The !! operator is a sharp tool:

  • It forces a nullable into a non-null type
  • It throws a NullPointerException if the value is null
  • Prefer safe calls and Elvis in almost all cases
fun main() {
    val sure: String? = "definitely here"
    println(sure!!.length)
    val maybe: String? = null
    println(maybe?.length ?: -1)
}

Quick Check

Test your understanding of the not-null assertion operator.

Recap

You learned about the !! operator:

  • It asserts a nullable value is not null
  • It throws a NullPointerException when wrong
  • It should be a last resort, not a habit

Next you will see how nullability arrives from Java through platform types.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “The !! Operator” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “The !! Operator”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

What will I learn in “The !! Operator”?

Force non-null and its risks. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “The !! Operator” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Nullable and Non-Null Types
  2. Safe Calls and Elvis
  3. The !! Operator
  4. Platform Types
← Back to Kotlin Academy