Introduction
One of Kotlinโs biggest advantages over Java is built-in null safety.
If you've written Android apps in Java before, you probably remember how often apps crashed because of a NullPointerException.
Kotlin tries to solve this problem by making nullability part of the type system.
This means the compiler can help prevent many null-related bugs before your code even runs.
In this guide weโll cover the most important Kotlin null safety tools:
๐ Korean Tip
์ฝํ๋ฆฐ์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ค ํ๋๊ฐ Null Safety ์
๋๋ค.
Java์์ ์์ฃผ ๋ฐ์ํ๋ NullPointerException ๋ฌธ์ ๋ฅผ ์ปดํ์ผ ๋จ๊ณ์์ ์ค์ฌ์ฃผ๋ ๊ธฐ๋ฅ์
๋๋ค.
What is Null Safety in Kotlin?
In Kotlin, variables are non-null by default.
This means the compiler will not allow you to assign null unless you explicitly say the variable can be nullable.
Example:
val name: String = "Kotlin"
This variable cannot be null.
If you want a variable that can hold null, you must add ?.
val name: String? = null
๐ Korean Tip
?๊ฐ ๋ถ์ผ๋ฉด null์ด ๋ค์ด๊ฐ ์ ์๋ ๋ณ์๊ฐ ๋ฉ๋๋ค.
Safe Call Operator (?.)
The safe call operator allows you to safely access properties of nullable objects.
Example
val name: String? = "Android"
println(name?.length)
If name is null, the expression simply returns null instead of crashing.
๐ Korean Tip
?. ์ฐ์ฐ์๋ null์ด๋ฉด ์คํํ์ง ์๊ณ null์ ๋ฐํํฉ๋๋ค.
Equivalent Java-style code would look like this:
if (name != null) {
println(name.length)
}
The Kotlin version is much cleaner.
Using let for Null Checks
One of the most common patterns in Kotlin is combining safe calls with let.
Example
val name: String? = "Kotlin"
name?.let {
println("Name is $it")
}
This block runs only if the value is not null.
๐ Korean Tip
?.let { } ํจํด์ ์ฝํ๋ฆฐ์์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ null ์ฒดํฌ ๋ฐฉ์์
๋๋ค.
The Not-Null Assertion Operator (!!)
Sometimes developers use !! to force a nullable value to be treated as non-null.
Example:
val name: String? = "Kotlin"
println(name!!.length)
If name happens to be null, the app will crash.
๐ Korean Tip
!!๋ null์ด ์๋๋ผ๊ณ ๊ฐ์ ๋ก ๋จ์ ํ๋ ์ฐ์ฐ์์
๋๋ค.
ํ์ง๋ง null์ด๋ฉด ์ฑ์ด ๋ฐ๋ก crashํฉ๋๋ค.
Developer Tip
Avoid using !! whenever possible.
Most experienced Kotlin developers consider frequent use of !! a code smell.
Elvis Operator (?:)
The Elvis operator provides a default value if something is null.
Example:
val name: String? = null
val result = name ?: "Guest"
println(result)
Output
Guest
๐ Korean Tip
?: ๋ null์ผ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์ ์ ๊ณตํ๋ ์ฐ์ฐ์์
๋๋ค.
Common Null Safety Patterns
Here are some patterns you'll see often in Kotlin code.
Pattern 1: Safe call
user?.email
Pattern 2: let block
user?.let {
sendEmail(it)
}
Pattern 3: Default value
val name = user?.name ?: "Guest"
๐ Korean Tip
์ด ์ธ ๊ฐ์ง ํจํด์ Android Kotlin ์ฝ๋์์ ๊ฑฐ์ ๋งค์ผ ์ฌ์ฉํ๋ ํจํด์
๋๋ค.
Common Mistakes Developers Make
Using !! too often
This defeats the purpose of Kotlin null safety.
Example:
val name: String? = null
println(name!!.length)
This will crash.ย
Forgetting nullable types
Sometimes developers forget to mark variables nullable.
Example:
val name: String = null
This will not compile.
Best Practices for Kotlin Null Safety
Prefer safe calls
user?.email
Use let for scoped logic
user?.let {
sendEmail(it)
}
Use Elvis operator for defaults
val username = user?.name ?: "Guest"
๐ Korean Tip
Kotlin Null Safety์ ํต์ฌ์
์ด ์ธ ๊ฐ์ง๋ฅผ ์ ํ์ฉํ๋ ๊ฒ์
๋๋ค.
Quick Kotlin Null Safety Cheat Sheet
| Feature |
Example |
Purpose |
| Nullable type |
String? |
allow null |
| Safe call |
?. |
avoid crash |
| let |
?.let {} |
null check |
| Elvis operator |
?: |
default value |
| Not-null assertion |
!! |
force non-null |
FAQ
Why does Kotlin care so much about null?
Because NullPointerException is one of the most common causes of app crashes.
Kotlinโs type system helps prevent these errors.
Should I ever use !!?
Only when you are absolutely sure the value cannot be null.
Even then, many developers prefer safer alternatives.
What is the most common null handling pattern?
This one:
value?.let { }
ย
ย