Null Safety
Kotlin is a modern, concise language whose signature feature is null safety — its type system eliminates the NullPointerExceptions that crash programs in other languages.
Learn Null Safety in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll understand nullable types, the safe call ?. , the Elvis operator ?: , and when (rarely) to use !! .
What You'll Learn in This Lesson
1️⃣ Nullable Types and Safe Calls
In Kotlin, a regular type like String can never be null. To allow null, you add a question mark: String? . Once a value might be null, the compiler refuses to let you use it carelessly — you must handle the null case. The safe call operator ?. returns null instead of crashing.
The key insight: the line that would crash in other languages doesn't even compile here. You're forced to deal with null up front, which is exactly why Kotlin programs crash so much less.
2️⃣ Elvis, !!, and let
The Elvis operator ?: supplies a fallback when the left side is null. The not-null assertion !! forces a value to non-null and throws if you're wrong — use it sparingly. And ?.let {' '} runs a block only when the value isn't null.
The pattern value?.length ?: 0 is everywhere in real Kotlin: "the length, or 0 if it's missing". Lean on it.
Your turn. Fill in the TODO , then run and compare.
Combine safe calls and the Elvis operator to print a profile that never crashes on missing data.
📋 Quick Reference — Null Safety
Practice quiz
How do you declare a String variable that is allowed to hold null?
- val s: String = null
- val s: String? = null
- val s: Nullable<String> = null
- val s: String! = null
Answer: val s: String? = null. Adding ? to the type (String?) makes it nullable; a plain String can never be null.
What does middle?.length evaluate to when middle is null?
- null
- 0
- It throws a NullPointerException
- An empty string
Answer: null. The safe call ?. returns null instead of crashing when the receiver is null.
What is the result of the expression (null as String?) ?: "default"?
- null
- an empty string
- "default"
- a NullPointerException
Answer: "default". The Elvis operator ?: supplies the right-hand value when the left side is null.
What does the not-null assertion operator !! do?
- Returns null safely when the value is absent
- Converts the value to a nullable type
- Forces a non-null value and throws NPE if it is actually null
- Provides a default value
Answer: Forces a non-null value and throws NPE if it is actually null. !! asserts non-null and throws a NullPointerException at runtime if you are wrong.
What does name?.length ?: 0 return when name is null?
- null
- 0
- It does not compile
- The length of an empty string
Answer: 0. The safe call yields null, then Elvis substitutes the fallback 0.
Which block runs only when a nullable value is non-null?
- value!!.let { }
- value?.let { }
- value.run { }
- value.also { } always runs
Answer: value?.let { }. value?.let { } combines a safe call with let so the block runs only for a present value.
After if (x != null) { ... }, how can you use x inside the block?
- Only with !!
- Only with ?.
- Directly, because Kotlin smart-casts x to non-null
- You must reassign it first
Answer: Directly, because Kotlin smart-casts x to non-null. A null check smart-casts a local val to its non-null type inside that block.
Why is Kotlin null safety often described as moving a problem to compile time?
- It removes all exceptions from the language
- The compiler forces you to handle null before the code compiles
- It makes every type nullable by default
- It runs a null checker only at runtime
Answer: The compiler forces you to handle null before the code compiles. Nullable types force handling at compile time rather than failing as a runtime NPE.
What does a chain like a?.b?.c return if b is null?
- The value of c
- null
- A NullPointerException
- The value of a
Answer: null. A chain of safe calls short-circuits to null as soon as any link is null.
Which is the recommended default for handling a nullable value?
- Always use !! for brevity
- Use ?. and ?: and reserve !! for rare certain cases
- Never check for null
- Cast away the nullability
Answer: Use ?. and ?: and reserve !! for rare certain cases. Prefer safe calls and Elvis; treat !! as a code smell used only when truly certain.