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
// 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, allowedExplanation
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
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
Length: 5
Length: -1Key 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
1. What happens if you try to assign null to a variable of type String in Kotlin?
2. How do you declare a variable that is allowed to hold null in Kotlin?
3. What is the primary benefit of Kotlin's compile-time null safety compared to Java?
4. Which operator forces a nullable value to be treated as non-null, throwing an exception if it is actually null?
Was this page helpful?
You May Also Like
Safe Calls and the Elvis Operator in Kotlin
The ?. safe call and ?: Elvis operator let you access members of nullable values and supply defaults without verbose null checks.
Smart Casts in Kotlin
After a null-check or type-check, Kotlin's compiler automatically treats a variable as the narrowed type within that scope, no explicit cast needed.
Variables and Data Types in Kotlin
Learn how Kotlin declares variables with val and var, and the built-in data types used to store values.
Kotlin-Java Interoperability
Explore how Kotlin and Java code can call each other seamlessly on the JVM, and the nuances that come with it.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics