kotlinbeginner
Kotlin Null Safety Complete Guide
Master null safety: safe calls, elvis operator, let/also/run, smart casts, and nullable collection handling.
kotlinPress ⌘/Ctrl + Shift + C to copy
fun main() {
var name: String? = "Alice"
var missing: String? = null
// Safe call operator ?.
println(name?.length) // 5
println(missing?.length) // null (no NPE)
// Safe call chain
val city = getUser()?.address?.city?.uppercase()
println("City: $city")
// Elvis operator ?: (default value)
val len = missing?.length ?: 0
println("Length: $len")
// let: execute block if non-null
name?.let { println("Name is: $it") }
missing?.let { println("Never printed") }
// let for transformation
val upper = name?.let { it.trim().uppercase() } ?: "UNKNOWN"
println("Upper: $upper")
// Smart cast after null check
if (name != null) {
println("Length: ${name.length}") // compiler knows it's String
}
// when with null
fun describe(value: String?): String = when {
value == null -> "null"
value.isBlank() -> "blank"
value.length < 3 -> "short"
else -> "normal: $value"
}
println(describe(null))
println(describe(""))
println(describe("hi"))
println(describe("hello"))
// Nullable collections
val items: List<String?> = listOf("a", null, "b", null, "c")
val nonNullItems: List<String> = items.filterNotNull()
println("Non-null: $nonNullItems")
// mapNotNull
val numbers = listOf("1", "abc", "3", "xyz", "5")
val parsed = numbers.mapNotNull { it.toIntOrNull() }
println("Parsed: $parsed") // [1, 3, 5]
// requireNotNull and checkNotNull
val config: String? = "production"
val env = requireNotNull(config) { "Config must be set" }
println("Env: $env")
// Safe cast
val obj: Any = "Hello"
val str: String? = obj as? String // safe cast
val num: Int? = obj as? Int // null, no ClassCastException
println("Cast: str=$str, num=$num")
// takeIf / takeUnless
val validated = name
?.takeIf { it.length >= 3 }
?.also { println("Valid: $it") }
println("Validated: $validated")
}
data class Address(val city: String?)
data class User2(val name: String, val address: Address?)
fun getUser(): User2? = User2("Alice", Address("NYC"))Use Cases
- Safe handling of nullable data from APIs
- Default value patterns with elvis operator
- Filtering and transforming nullable collections
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
kotlinbeginner
Null Safety — Elvis, Safe Call, and let
Master Kotlin null safety: safe calls, Elvis operator, let/also scoping, and smart casts.
Best for: Safe navigation through nullable chains
#kotlin#null-safety
kotlinbeginner
Data Classes — Copy, Destructure, and Equals
Use data classes for immutable models: auto-generated equals, hashCode, copy, and destructuring.
Best for: Immutable domain models and DTOs
#kotlin#data-class
kotlinbeginner
Scope Functions — let, run, apply, also, with
Master Kotlin scope functions: when to use let, run, apply, also, and with for concise code.
Best for: Object initialization and configuration
#kotlin#scope-functions
kotlinbeginner
Collections — map, filter, groupBy, and More
Master Kotlin collections: functional transformations, aggregations, grouping, and partition operations.
Best for: Data processing and transformation pipelines
#kotlin#collections