What if your app could avoid crashes just by using a tiny symbol?
Why Safe call operator (?.) in Kotlin? - Purpose & Use Cases
Imagine you have a list of friends, and you want to check their phone numbers. But some friends might not have a phone number saved. If you try to get the phone number directly, your program might crash because it found nothing.
Without the safe call operator, you have to write many checks like "if this friend exists, then check if the phone number exists". This makes your code long, hard to read, and easy to forget checks, causing errors or crashes.
The safe call operator (?.) lets you ask for a property or call a function only if the object is not null. If it is null, it just returns null safely without crashing. This makes your code shorter, cleaner, and safer.
if (friend != null) { if (friend.phoneNumber != null) { println(friend.phoneNumber) } }
println(friend?.phoneNumber)
It enables you to write simple, safe code that handles missing or null data gracefully without crashes.
When building a contact app, you can safely show a friend's phone number if it exists, or show nothing if it doesn't, without worrying about errors.
Manual null checks make code long and error-prone.
The safe call operator (?.) simplifies null handling.
It helps avoid crashes by safely accessing nullable data.
Practice
?. do in Kotlin?Solution
Step 1: Understand the purpose of the safe call operator
The safe call operator?.is used to safely access properties or call functions on objects that might be null.Step 2: Behavior when object is null
If the object is null, the operator returns null instead of throwing an error or crashing the program.Final Answer:
It accesses a property or calls a function only if the object is not null. -> Option CQuick Check:
Safe call operator = Access if not null [OK]
- Thinking it crashes on null
- Confusing with non-null assertion operator (!!)
- Assuming it provides default values
length property of a nullable String name?Solution
Step 1: Identify the safe call syntax
The safe call operator is?., used asobject?.propertyorobject?.function().Step 2: Check each option
val len = name.length uses.without safety, which can cause a crash ifnameis null. val len = name?.length correctly uses?.. val len = name!!length uses!!incorrectly with property access. val len = name?length uses invalid syntax?length.Final Answer:
val len = name?.length -> Option BQuick Check:
Safe call syntax = ?. [OK]
- Using . without safe call on nullable
- Confusing !! with ?.
- Writing invalid syntax like ?property
val text: String? = null println(text?.length)
Solution
Step 1: Analyze the nullable variable
The variabletextis null, so accessingtext?.lengthuses the safe call operator.Step 2: Evaluate the safe call result
Sincetextis null,text?.lengthreturns null instead of crashing or returning 0.Final Answer:
null -> Option DQuick Check:
Null safe call on null = null [OK]
- Expecting 0 as length of null
- Thinking it throws an exception
- Assuming empty string output
val user: User? = null val nameLength = user.length?.toString()
Solution
Step 1: Identify the nullable object and property access
useris nullable, so accessinglengthdirectly without safe call is unsafe.Step 2: Correct safe call placement
The safe call operator must be used on the nullable object:user?.length. The codeuser.length?.toString()tries to accesslengthon a nullable object without safety, causing an error.Final Answer:
The safe call operator should be beforelength, likeuser?.length. -> Option AQuick Check:
Safe call must be on nullable object [OK]
- Using safe call after property instead of before
- Ignoring nullability of variable
- Misunderstanding function call on nullable
val items: List?, which expression safely gets the length of the first string or returns null if any part is null?Solution
Step 1: Understand the nullability
itemscan be null, and its elements can also be null. So both the list and the first element might be null.Step 2: Use safe calls to avoid errors
To safely get the length of the first string, useitems?.get(0)?.length. This returns null ifitemsor the first element is null.Final Answer:
items?.get(0)?.length -> Option AQuick Check:
Chain safe calls for nested nullables [OK]
- Using !! which throws exception if null
- Missing safe call on list or element
- Using invalid syntax like ?[0]
Ready to prove it?
Pass the test and this pattern moves unknown
