The safe call operator (?.) helps you avoid errors when working with values that might be null. It lets you safely access properties or call functions without crashing your program.
Safe call operator (?.) in Kotlin
val result = someObject?.propertyOrFunction()The operator ?. checks if someObject is not null before accessing the property or calling the function.
If someObject is null, the whole expression returns null instead of causing an error.
name only if person is not null.val name: String? = person?.name
text if it is not null, otherwise length will be null.val length = text?.lengthtext safely if text is not null.val firstChar = text?.get(0)
This program creates a Person object and safely gets the length of the name using the safe call operator. It also shows what happens when the person is null.
fun main() { val person: Person? = Person("Alice") val nameLength = person?.name?.length println("Name length: $nameLength") val noPerson: Person? = null val noNameLength = noPerson?.name?.length println("Name length when person is null: $noNameLength") } data class Person(val name: String)
The safe call operator helps prevent NullPointerException errors.
You can chain multiple safe calls like obj?.a?.b?.c.
If you want to provide a default value when null, combine with the Elvis operator: obj?.property ?: defaultValue.
The safe call operator (?.) lets you access properties or call functions only if the object is not null.
If the object is null, the expression returns null instead of crashing.
This makes your code safer and easier to read when dealing with nullable values.
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
