← All notes

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

Pitfalls

Claims

Sources

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

Permanent ID: iil:note:08t5s1aa

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.},
}

Markdown

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.