Kotlin Null Safety

Eliminate NullPointerExceptions in Kotlin codebases using nullable types, safe calls, and smart casts.

187|20|Updated Nov 20, 2025
One-click install
npx skills add https://github.com/TheBushidoCollective/han --skill kotlin-null-safety
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Kotlin Null Safety
Source: https://github.com/TheBushidoCollective/han/tree/main/jutsu/jutsu-kotlin/skills/kotlin-null-safety
Command: npx skills add https://github.com/TheBushidoCollective/han --skill kotlin-null-safety

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Kotlin null safety enforces explicit null handling, reducing runtime crashes and improving code robustness.

Core Features & Use Cases

  • Nullable Types: Distinguish between nullable and non-nullable.
  • Safe Calls & Elvis: Handle nulls concisely and safely.
  • Smart Casts: Narrow types after null checks.

Quick Start

Refactor a function returning String? to safely provide a non-null String using a default.

Frequently Asked Questions about Kotlin Null Safety

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I eliminate NullPointerExceptions in Kotlin?

Kotlin eliminates NullPointerExceptions by enforcing nullable and non-nullable types at compile time. Declare types as nullable (String?) or non-nullable (String), use safe calls (?.), the Elvis operator (??), and smart casts to handle nulls safely without runtime crashes.

What's the difference between nullable and non-nullable types in Kotlin?

Nullable types (String?) can hold null values; non-nullable types (String) cannot. Kotlin enforces this distinction at compile time, preventing null dereferences. Non-nullable types are the default, making null handling explicit and deliberate.

How do I use the Elvis operator and safe calls in Kotlin?

Safe calls (?.) invoke methods only if the object is non-null, returning null otherwise. The Elvis operator (??) provides a default value if the left side is null. Together, they allow concise null handling: val length = name?.length ?: 0 returns the string length or zero if null.

Can I refactor existing code to use Kotlin null safety?

Yes. Refactor functions returning nullable types (String?) to use safe calls, Elvis operators, or smart casts to provide non-null returns with defaults. This preserves code expressiveness while enforcing compile-time null safety and eliminating runtime crashes.

When should I use nullable types versus non-nullable types?

Use non-nullable types by default for variables that always have values. Use nullable types only when a value may legitimately be absent. This explicit distinction makes null handling intentional and reduces accidental dereferencing of null values.

How does Kotlin's smart cast prevent null pointer errors?

After explicit null checks (if (x != null)), Kotlin smart casts the variable to its non-nullable type automatically within that scope. This eliminates redundant null checks and safely narrows types, preventing null dereference in subsequent code.