Kotlin null safety and requireNotNull
Short answer
Kotlin supports nullability as part of its type system, and the compiler enforces that non-null variables cannot hold a null value [S1]. You handle nullable values with the safe call operator (?.), the Elvis operator (?:), and smart casts [S1]. requireNotNull() throws an IllegalArgumentException if the value is null; otherwise it returns the not null value [S2].
fun processName(name: String?) {
val validName = requireNotNull(name) { "Name must not be null" }
println("User: ${validName.uppercase()}")
}
Key points
- You can declare which variables or properties are allowed to be null. A regular
Stringcannot hold null. [S1] - The safe call operator
?.handles nullability in a shorter form. If the object is null, the operator returns null instead of throwing an NPE. [S1] - The Elvis operator returns the left-hand side when it is not null. The right-hand side is evaluated only if the left-hand side is null. [S1]
- The not-null assertion operator
!!converts any value to a non-nullable type. A null value after!!is treated as non-nullable and results in an NPE. [S1] requireNotNull()throws anIllegalArgumentExceptionif the value is null. Otherwise it returns the not null value. [S2]
Pitfalls
- Applying
!!to a null value treats it as non-nullable and results in an NPE. [S1] - Java interoperation includes attempts to access a member of a null reference of a platform type. [S1]
Claims
- id: C1 claim: Kotlin explicitly incorporates nullability into its type system to prevent null pointer exceptions. source: S1 evidence: "Kotlin explicitly supports nullability as part of its type system"
- id: C2 claim: The safe call operator returns null instead of throwing an exception when evaluating a null receiver. source: S1 evidence: "if the object is
null, the?.operator simply returnsnull:"
- id: C3 claim: The not-null assertion operator converts any value directly to a non-nullable type. source: S1 evidence: "converts any value to a non-nullable type."
- id: C4 claim: The standard library requireNotNull function returns the value when it is not null. source: S2 evidence: "Otherwise returns the not null value."
- id: C5 claim: A requireNotNull check without a custom message uses a default failure message. source: S2 evidence: "Required value was null."
Sources
- [S1] Null safety - Kotlin Documentation. https://kotlinlang.org/docs/null-safety.html . License: Apache-2.0. Accessed 2026-11-11.
- [S2] Preconditions.kt - Kotlin Standard Library. https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/src/kotlin/util/Preconditions.kt . License: Apache-2.0. Accessed 2026-11-11.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources and notices: Kotlin documentation (JetBrains/kotlin-web-site): Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors; Apache-2.0 (https://raw.githubusercontent.com/JetBrains/kotlin-web-site/master/LICENSE). Kotlin standard library (JetBrains/kotlin): Copyright 2010-2024 JetBrains s.r.o. and respective authors and developers; Apache-2.0 (https://raw.githubusercontent.com/JetBrains/kotlin/master/license/LICENSE.txt). Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Kotlin docs, Apache-2.0 (kotlinlang.org); Preconditions.kt, Apache-2.0. Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors. Notices: Kotlin documentation (JetBrains/kotlin-web-site): Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors; Apache-2.0 (https://raw.githubusercontent.com/JetBrains/kotlin-web-site/master/LICENSE). Kotlin standard library (JetBrains/kotlin): Copyright 2010-2024 JetBrains s.r.o. and respective authors and developers; Apache-2.0 (https://raw.githubusercontent.com/JetBrains/kotlin/master/license/LICENSE.txt).
Cite this
iinventedlight. 2026. "Kotlin null safety and requireNotNull." iinventedlight Library, iil:note:08t5s1aa, version of 2026-11-11. https://iinventedlight.com/library/notes/kotlin-null-safety/. License: CC-BY-4.0.
@misc{iil_note_08t5s1aa,
author = {{iinventedlight}},
title = {{Kotlin null safety and requireNotNull}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-11},
url = {https://iinventedlight.com/library/notes/kotlin-null-safety/},
note = {Permanent ID iil:note:08t5s1aa. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/kotlin-null-safety.md.