100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Null Safety in Kotlin

Kotlin's type system distinguishes nullable from non-nullable types at compile time, eliminating most NullPointerExceptions before code ever runs.

Null SafetyBeginner9 min readJul 8, 2026
Analogies

Introduction

Null pointer exceptions (NPEs) have long been called the 'billion-dollar mistake' in languages like Java, where any reference type can silently hold null and blow up at runtime. Kotlin attacks this problem at the type-system level: every type is either nullable (can hold null) or non-nullable (can never hold null), and the compiler enforces the distinction. This means a huge class of NPEs simply cannot compile, catching bugs at build time instead of in production.

🏏

Cricket analogy: Like a scoring app that used to crash mid-match whenever a substitute fielder's name field was left blank, Java's silent nulls caused runtime failures, while Kotlin's compiler flags an empty player slot before the match ever starts.

Syntax

kotlin
// Non-nullable type: can never hold null
var name: String = "Kotlin"
name = null // Compile error!

// Nullable type: marked with a trailing '?'
var nickname: String? = "Kt"
nickname = null // OK, allowed

Explanation

Appending a question mark to a type, like String?, tells the compiler that a variable of that type may hold either a valid value or null. A plain String, by contrast, is guaranteed by the compiler to never be null, so you can call methods on it directly without defensive null checks. Because the two kinds of types are incompatible, the compiler forces you to explicitly handle the nullable case wherever it appears, most commonly with the safe call operator ?. or the Elvis operator ?:, or by using a non-null assertion !! when you are certain a value is not null (though this reintroduces NPE risk and should be used sparingly).

🏏

Cricket analogy: Like a scorecard field marked 'Man of the Match: TBD?' that might stay empty until the presentation ceremony, versus 'Winning Team' which is always guaranteed filled in, Kotlin's String? allows null while a plain String never can, forcing explicit checks (?., ?:) or a confident !! when you're certain, like announcing the winner before the ceremony.

Example

kotlin
fun describeLength(text: String?) {
    // Cannot call text.length directly; text might be null
    val length = text?.length ?: -1
    println("Length: $length")
}

fun main() {
    describeLength("Hello")
    describeLength(null)
}

Output

kotlin
Length: 5
Length: -1

Key Takeaways

  • Kotlin types are non-nullable by default; add ? to permit null, e.g. String?.
  • A non-nullable variable can never be assigned null, so the compiler rejects it at build time.
  • Nullable types must be unwrapped safely before most operations, usually with ?. or ?:.
  • The !! operator forces a nullable to non-null but throws a KotlinNullPointerException if the value is actually null.
  • This design eliminates most NullPointerExceptions that are common in languages like Java.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#NullSafetyInKotlin#Null#Safety#Syntax#Explanation#StudyNotes#SkillVeris