0Pricing
Kotlin Multiplatform Academy · Lesson

Null Safety in Shared Models

Use Kotlin nullability to prevent cross-platform crashes.

Null Safety in Shared Models is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Crashes You Can Prevent

A missing value is a top cause of app crashes. Kotlin's null safety lets shared models guard against it on both platforms.

Non-Null by Default

A normal type can never hold null. Declaring a field as String guarantees it always has a real value.

data class User(val name: String)

The Nullable Type

Add a question mark to allow null. This marks a field as optional, so the compiler forces you to handle the empty case.

data class User(val name: String, val nickname: String?)

Safe Call Operator

Use ?. to read a nullable field safely. If the value is null, the whole expression yields null instead of crashing.

val length = user.nickname?.length

Provide a Fallback

The Elvis operator ?: supplies a default when the value is null, so downstream code always gets something usable.

val shown = user.nickname ?: "Anonymous"

Default Values in Models

Give a constructor parameter a default so callers can skip optional fields entirely while keeping the model non-null.

data class User(val name: String, val active: Boolean = true)

Smart Casts After a Check

Once you confirm a value is not null, Kotlin smart casts it. Inside the check you use it as a plain non-null type.

if (user.nickname != null) {
  println(user.nickname.length)
}

Avoid the !! Operator

The !! operator forces a value to non-null and crashes if it is null. Reach for safe calls and Elvis instead.

val risky = user.nickname!!.length // can crash

Model Optionality Honestly

Mark a field nullable only when it truly can be absent. Clear types document your domain for both teams.

data class Profile(val bio: String?, val avatar: String?)

Why This Matters Cross-Platform

Null safety lives in commonMain, so Android and iOS inherit the same protection from the very same model. 🙂

One Source of Truth

Because nullability rules sit in shared code, neither app can accidentally treat an optional field as required.

Quick Check

How should you read a nullable field with a fallback?

Recap

Mark optional fields with ?, read them via safe calls and Elvis, and avoid !!, so shared models stay crash-safe on both apps.

Frequently asked questions

Is the “Null Safety in Shared Models” lesson free?

Yes — the full text of “Null Safety in Shared Models” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Null Safety in Shared Models”?

Use Kotlin nullability to prevent cross-platform crashes. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Null Safety in Shared Models” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Data Classes for Your Domain
  2. Enums & Sealed Classes for State
  3. Null Safety in Shared Models
  4. Validation Helpers on Your Models
← Back to Kotlin Multiplatform Academy