0
0
Kotlinprogramming~10 mins

Null safety in collections in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Null safety in collections
Declare collection
Specify element type
Allow or disallow nulls?
YesCollection holds nulls
Null safety enforced
Use collection safely
This flow shows how Kotlin collections can be declared to allow or disallow null elements, enforcing null safety.
Execution Sample
Kotlin
val list1: MutableList<String> = mutableListOf("a", "b")
val list2: MutableList<String?> = mutableListOf("a", null, "b")
val list3: MutableList<String?> = mutableListOf(null, null)
val list4: MutableList<String> = mutableListOf()
This code declares Kotlin lists with and without null elements to show null safety in collections.
Execution Table
StepCode LineActionCollection TypeAllows Null?Content
1val list1: MutableList<String> = mutableListOf("a", "b")Create list1MutableList<String>No["a", "b"]
2val list2: MutableList<String?> = mutableListOf("a", null, "b")Create list2MutableList<String?>Yes["a", null, "b"]
3val list3: MutableList<String?> = mutableListOf(null, null)Create list3MutableList<String?>Yes[null, null]
4val list4: MutableList<String> = mutableListOf()Create list4MutableList<String>No[]
5Access list1[1]Retrieve elementMutableList<String>No"b"
6Access list2[1]Retrieve elementMutableList<String?>Yesnull
7Try to add null to list1Compile errorMutableList<String>NoError: Null not allowed
8Add null to list2AllowedMutableList<String?>Yes["a", null, "b", null]
💡 Execution stops because Kotlin enforces null safety at compile time; adding null to non-null collections is not allowed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 8
list1uninitialized["a", "b"]["a", "b"]["a", "b"]["a", "b"]["a", "b"]
list2uninitializeduninitialized["a", null, "b"]["a", null, "b"]["a", null, "b"]["a", null, "b", null]
list3uninitializeduninitializeduninitialized[null, null][null, null][null, null]
list4uninitializeduninitializeduninitializeduninitialized[][]
Key Moments - 3 Insights
Why can't we add null to list1 which is List<String>?
Because list1 is declared as List<String>, it does not allow null elements. Kotlin enforces this at compile time, so adding null causes an error (see step 7 in execution_table).
How can list2 hold null values?
list2 is declared as List<String?>, meaning its elements can be String or null. This allows nulls safely, as shown in steps 2 and 8.
What happens if we try to access a null element in list2?
Accessing a null element returns null safely (step 6). Kotlin forces you to handle this possibility to avoid crashes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of list2 after step 8?
A["a", null, "b", null]
B["a", "b", null]
C["a", null, "b"]
D[null, null]
💡 Hint
Check the 'Content' column at step 8 in execution_table.
At which step does Kotlin prevent adding null to a non-null list?
AStep 2
BStep 4
CStep 7
DStep 6
💡 Hint
Look for the 'Compile error' action in execution_table.
According to variable_tracker, what is the value of list3 after step 3?
A["a", null, "b"]
B[null, null]
C[]
D["a", "b"]
💡 Hint
Check the row for list3 under 'After Step 3' in variable_tracker.
Concept Snapshot
Null safety in Kotlin collections:
- Declare collection type with or without nullable elements (e.g., List<String> vs List<String?>)
- Non-null collections disallow null elements at compile time
- Nullable collections allow nulls safely
- Accessing nullable elements requires null checks
- Kotlin enforces these rules to prevent runtime null errors
Full Transcript
This lesson shows how Kotlin handles null safety in collections. Collections can be declared to hold only non-null elements or to allow nulls by using nullable types. For example, List<String> cannot hold nulls, while List<String?> can. The execution table traces creating such lists, accessing elements, and attempts to add nulls. Kotlin prevents adding null to non-null collections at compile time, ensuring safety. Variable tracking shows how collections change over steps. Key moments clarify common confusions about null allowance and access. The visual quiz tests understanding of collection contents and Kotlin's compile-time checks. Overall, Kotlin's null safety in collections helps avoid null pointer errors by design.