Null Safety
Understand Kotlin's null safety system: nullable types, the safe call operator, the Elvis operator, and how to eliminate NullPointerExceptions.
Null Safety is a free Android Academy lesson on CoddyKit — lesson 1 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 Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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, guaranteedString?— 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.
Frequently asked questions
Is the “Null Safety” lesson free?
Yes — the full text of “Null Safety” is free to read here on the web, and the Android 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 Android Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
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.