📖 Scenario: Imagine you have a list of names that might contain some null values. You want to greet each person only if their name is not null. Using Kotlin's let function with safe calls helps you do this safely and cleanly.
🎯 Goal: Build a Kotlin program that uses the let function with safe calls to print greetings only for non-null names.
📋 What You'll Learn
Create a list called names with some String? values including null
Create a variable called greetingPrefix with the value "Hello"
Use a for loop to go through each name in names
Inside the loop, use a safe call with let to print a greeting only if the name is not null
💡 Why This Matters
🌍 Real World
Handling data that might be missing or null is common in apps. Using safe calls with let helps avoid errors and keeps code clean.
💼 Career
Many Kotlin jobs require safe handling of nullable data to prevent crashes and bugs, especially in Android development.
Progress0 / 4 steps
1
Create the list of names
Create a list called names with these exact values: "Alice", null, "Bob", null, "Charlie"
Kotlin
Hint
Use listOf to create the list and include null where specified.
2
Add a greeting prefix
Create a variable called greetingPrefix and set it to the string "Hello"
Kotlin
Hint
Use val to create an immutable variable for the greeting prefix.
3
Use let with safe calls to greet non-null names
Write a for loop using name to iterate over names. Inside the loop, use a safe call with name?.let to print "$greetingPrefix, $it!" only if name is not null
Kotlin
Hint
Use name?.let { ... } to run code only when name is not null.
4
Print the greetings
Run the program to print greetings for all non-null names using the println statement inside the let block
Kotlin
Hint
Make sure the println is inside the let block to print only for non-null names.
Practice
(1/5)
1. What does the Kotlin expression nullableVar?.let { println(it) } do?
easy
A. Prints the value of nullableVar only if it is not null.
B. Always prints null regardless of nullableVar.
C. Throws a NullPointerException if nullableVar is null.
D. Assigns nullableVar to it without printing.
Solution
Step 1: Understand safe call operator usage
The ?. operator checks if nullableVar is not null before proceeding.
Step 2: Analyze the let function behavior
If nullableVar is not null, let executes the lambda, printing the value.
Final Answer:
Prints the value of nullableVar only if it is not null. -> Option A
Quick Check:
?.let runs code only if not null [OK]
Hint: Remember: ?.let runs code only when value is not null [OK]
Common Mistakes:
Thinking it prints null when variable is null
Assuming it throws an exception on null
Confusing let with always running code
2. Which of the following is the correct syntax to safely print a nullable string name using let?
easy
A. name?.let { println(it) }
B. name.let { println(it) }
C. name?.let println(it)
D. name?.let { println(name) }
Solution
Step 1: Identify safe call usage
To avoid null errors, ?.let must be used, not just .let.
Step 2: Check lambda syntax correctness
The lambda must be enclosed in braces { } and use it to refer to the value.
Final Answer:
name?.let { println(it) } -> Option A
Quick Check:
Safe call + let with braces = correct syntax [OK]
Hint: Use ?.let { } with braces and 'it' inside [OK]
The variable number holds 5, which is not null, so ?.let runs.
Step 2: Calculate inside let block
The lambda multiplies it (5) by 2 and prints 10.
Final Answer:
10 -> Option B
Quick Check:
Non-null value runs let, prints doubled value [OK]
Hint: If variable not null, let runs and prints result [OK]
Common Mistakes:
Expecting no output if value is not null
Confusing printed value with original variable
Thinking it prints null when variable is non-null
4. Identify the error in this Kotlin code snippet:
val text: String? = null
text.let { println(it.length) }
medium
A. It prints 0 because null is treated as empty string.
B. It compiles but does not print anything.
C. It throws NullPointerException because safe call is missing.
D. It prints 'null' as a string.
Solution
Step 1: Check safe call usage
The code uses text.let without ?., so it runs even if text is null.
Step 2: Understand null dereference
Inside the lambda, it.length is called on null, causing a NullPointerException.
Final Answer:
It throws NullPointerException because safe call is missing. -> Option C
Quick Check:
Missing ?. causes null error [OK]
Hint: Always use ?.let to avoid null errors [OK]
Common Mistakes:
Assuming let skips null without safe call
Thinking null acts like empty string
Ignoring possibility of NullPointerException
5. Given a nullable list of strings val names: List<String>? = listOf("Anna", "Bob", null, "Cara"), which Kotlin code safely prints only non-null names using let with safe calls?
hard
A. names?.let { it.forEach { println(it) } }
B. names.forEach { it.let { println(it) } }
C. names?.forEach { println(it) }
D. names?.forEach { it?.let { println(it) } }
Solution
Step 1: Handle nullable list safely
Use names?.forEach to avoid errors if names is null.
Step 2: Filter out null elements inside the list
Inside the loop, use it?.let to print only non-null names.
Final Answer:
names?.forEach { it?.let { println(it) } } -> Option D
Quick Check:
Safe calls on list and elements print only non-null [OK]
Hint: Use ?.forEach and it?.let to skip nulls safely [OK]
Common Mistakes:
Not using safe call on list causing errors
Printing null elements without filtering
Using let without safe call on nullable elements
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.