0Pricing
Kotlin Academy · Lesson

The !! Operator

Force non-null and its risks.

The !! Operator is a free Kotlin Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “The !! Operator” lesson free?

Yes — the full text of “The !! Operator” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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