Android Academy · Lesson

Null Safety

Understand Kotlin's null safety system: nullable types, the safe call operator, the Elvis operator, and how to eliminate NullPointerExceptions.

Lesson 1 of 411 steps

Null Safety is a free Android Academy lesson on CoddyKit. This is lesson 1 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 Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

The Billion-Dollar Mistake

In Java, any variable can be null, causing a NullPointerException (NPE) at runtime — one of the most common crashes in Android apps.

Tony Hoare, who invented null, called it his billion-dollar mistake.

Kotlin eliminates NPEs at compile time with its null safety system.

Nullable vs Non-Nullable

In Kotlin, types are non-nullable by default. To allow null, add ? to the type:

  • String — never null, guaranteed
  • String? — can be null

The compiler enforces this — it won't let you assign null to a non-nullable type.

Nullable Types Example

The compiler catches null mistakes before your app runs:

fun main() {
    val name: String = "Alice"   // never null
    // name = null  // COMPILE ERROR

    var nickname: String? = "Al"  // nullable
    nickname = null               // OK

    println(name)
    println(nickname)  // null
}

Safe Call Operator ?.

The safe call operator ?. calls a method only if the object is not null. If it is null, it returns null instead of crashing.

nickname?.length → returns the length, or null if nickname is null.

Safe Calls in Action

Use ?. to safely access nullable properties:

fun main() {
    var text: String? = "Hello"
    println(text?.length)   // 5
    println(text?.uppercase()) // HELLO

    text = null
    println(text?.length)   // null (no crash!)
    println(text?.uppercase()) // null
}

Elvis Operator ?:

The Elvis operator ?: provides a default value when an expression is null:

val len = text?.length ?: 0

Read it as: "the length of text, or 0 if text is null".

Named after the Elvis emoji 🕺 whose hair looks like ?:.

Elvis Operator Example

Provide safe defaults with ?::

fun main() {
    var name: String? = "Bob"
    println(name?.length ?: 0)  // 3

    name = null
    println(name?.length ?: 0)  // 0

    val display = name ?: "Anonymous"
    println(display)  // Anonymous
}

Null Checks with if

Inside an if (x != null) block, Kotlin smart-casts the nullable type to non-nullable automatically — no need to use ?. inside the block.

Smart Cast Example

Kotlin narrows the type after a null check:

fun printLength(text: String?) {
    if (text != null) {
        // 'text' is now String (non-nullable) here
        println("Length: ${text.length}") // no ?. needed
    } else {
        println("text is null")
    }
}

fun main() {
    printLength("Kotlin")  // Length: 6
    printLength(null)       // text is null
}

Quick Check

What does null?.length ?: -1 evaluate to?

Recap: Null Safety

Kotlin's null safety system eliminates NPEs:

  • Types are non-nullable by default; add ? to allow null
  • ?. safe call — returns null instead of crashing
  • ?: Elvis operator — provide a default when null
  • Smart casts inside if (x != null) blocks

Next: structuring code with classes and objects.

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
36
Lessons
152

Frequently Asked Questions

Is the “Null Safety” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Null Safety”, 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 Android Academy course includes 4 lessons in total.

What will I learn in “Null Safety”?

Understand Kotlin's null safety system: nullable types, the safe call operator, the Elvis operator, and how to eliminate NullPointerExceptions. You practise Android 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 Android Academy?

No prior experience is required. Android 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 1 of 4.

How long does the “Null Safety” 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 Android Academy lesson?

Yes. Every Android 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. Null Safety
  2. Classes & Objects
  3. Data Classes & Sealed Classes
  4. Extension Functions & Higher-Order Functions
← Back to Android Academy