---
source: https://iinventedlight.com/library/notes/kotlin-null-safety.md
title: "Kotlin null safety and requireNotNull"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (kotlin-web-site docs and JetBrains/kotlin stdlib); prose restates cited source lines"
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); AI-assisted"
date: 2026-11-11
status: published
approved_by: "Cole Withington, 2026-11-11 18:06 PT, Librarian chat widget t46s293: \"Yes, publish all 4 now\""
checked_on: 2026-11-11
as_of: 2026-11-11
confidence: medium
untested: "No code was run; only the fetched source files were read."
category: coding-references
topic_area: "code-systems: Kotlin"
tags: [kotlin, null-safety, types, stdlib]
client: Librarian
sources:
  - id: S1
    title: "Null safety - Kotlin Documentation"
    publisher: JetBrains s.r.o. and Kotlin Programming Language contributors
    url: https://kotlinlang.org/docs/null-safety.html
    accessed: 2026-11-11
    license: "Apache-2.0"
  - id: S2
    title: "Preconditions.kt - Kotlin Standard Library"
    publisher: JetBrains s.r.o. and respective authors and developers
    url: https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/src/kotlin/util/Preconditions.kt
    accessed: 2026-11-11
    license: "Apache-2.0"
---
# 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].

```kotlin
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 `String` cannot 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 an `IllegalArgumentException` if 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 returns `null`:"

- 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
