Set your Publisher ID in src/config/ads.ts to display live ads.
Null Safety & The Billion-Dollar Mistake
Nullable types ?, safe call ?., Elvis operator ?:, and non-null assertion !!
Tony Hoare invented the null pointer in 1965, later calling it his "billion-dollar mistake." Kotlin eliminates NullPointerExceptions at compile-time through its type system.
The Null Safety Operators
Non-nullable by default: var name: String = "Alice" cannot hold null. Attempting to set name = null fails compilation!
Nullable Types (?): To allow null, append a question mark: var name: String? = null.
Safe Call (?.): Executes method only if the reference is non-null: name?.length returns null instead of throwing an exception.
Elvis Operator (?:): Provides fallback default values: val len = name?.length ?: 0.
Non-null assertion (!!): Forces unwrap; throws an NPE if null. Avoid in production!
fun printUserLength(username: String?) {
// Safe call + Elvis operator fallback
val length = username?.length ?: 0
println("Username '$username' has length: $length")
}
printUserLength("AdaLovelace") // 11
printUserLength(null) // 0 (No NullPointerException!)Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
Which operator in Kotlin provides a fallback default value if an expression evaluates to null?