Kotlin Null Safety Cheat Sheet
Covers nullable types, the safe call and elvis operators, the not-null assertion, and platform types for handling null safely in Kotlin.
1 PageBeginnerMar 28, 2026
Nullable Types
The ? suffix marks a type as allowed to hold null.
kotlin
var name: String = "Kotlin" // non-null, cannot be assigned null// name = null // compile errorvar nickname: String? = "Kt" // nullable, marked with ?nickname = null // OKfun greet(name: String) { // parameter cannot be null println("Hello, $name")}fun greetSafe(name: String?) { // parameter may be null println("Hello, ${name ?: "Guest"}")}
Safe Calls & Elvis Operator
Chaining nullable access without throwing.
kotlin
val nickname: String? = null// Safe call: returns null instead of throwing if the receiver is nullval length: Int? = nickname?.length// Elvis operator: provide a default when the left side is nullval displayName: String = nickname ?: "Anonymous"// Chaining safe callsdata class Address(val city: String?)data class User(val address: Address?)val user: User? = User(Address(null))val city = user?.address?.city ?: "Unknown"// Elvis with early return/throwfun process(value: String?) { val v = value ?: return println(v.uppercase())}
Not-Null Assertion
Forcing an unwrap when you're certain a value is non-null.
kotlin
val nickname: String? = "Kt"// !! throws NullPointerException if the value is actually nullval length: Int = nickname!!.length// Use sparingly -- only when you are certain the value cannot be null// and a crash is the correct behavior if that assumption is wrong.fun getConfig(): String? = readConfigFile()// Prefer requireNotNull/checkNotNull for clearer failure messagesval config: String = requireNotNull(getConfig()) { "Config must be present" }
Null Safety Operators
The full operator toolkit for working with nullable types.
- ? (nullable marker)- Declares that a type may hold null, e.g. String?
- ?. (safe call)- Calls a member only if the receiver is non-null; otherwise evaluates to null
- ?: (elvis operator)- Supplies a default value when the left-hand expression is null
- !! (not-null assertion)- Forces unwrap; throws NullPointerException if the value is null
- ?.let { }- Executes the block only when the receiver is non-null, using it as the argument
- as? (safe cast)- Casts to a type, returning null instead of throwing ClassCastException on failure
- lateinit var- Defers initialization of a non-null var, avoiding a nullable type for late-bound properties
Smart Casts
The compiler automatically treats a checked nullable as non-null.
kotlin
fun describe(value: String?) { if (value != null) { // Kotlin smart-casts value to non-null String inside this block println(value.length) } // Also works with early return if (value == null) return println(value.uppercase())}// Smart casts don't work across mutable var properties that could change,// but work reliably with local val and immutable properties.
Pro Tip
Avoid `!!` in application code — it reintroduces the exact NullPointerException risk Kotlin's type system is designed to eliminate. Prefer `?.`, `?:`, or `requireNotNull()` with a descriptive message so failures are self-explanatory.
Was this cheat sheet helpful?
Explore Topics
#KotlinNullSafety#KotlinNullSafetyCheatSheet#Programming#Beginner#NullableTypes#Safe#Calls#Elvis#Testing#CheatSheet#SkillVeris