Kotlin’s type system is aimed at eliminating the code’s risk of null reference because it is a very common and easily produceable error. NullPointerException is a very common type of error or exception easily encounterd by a programmer while programming in java or similar language. The main drawback of NullPointerException is that it is not detectable during compile time and it is thrown only during runtime wich causes application failure or system crashes. Kotlin compiler also throws NullPointerException when it detects a null reference.
NullPointerException’s can be cause due to one of the following reasons
- Explicit call to throw NullPointerException()
- Use of the not-null assertion operato(!!) operator
- Data inconsistency with respect to initialization e.g. an uninitialized this is passed as an argument.
- If you try to access a member on a null reference
Nullable and Non-Nullable Types in Kotlin
Kotlin framework has two types of references which can hold null (nullable references) and those which can not hold null(non-null references).
From the example below a variable of type String can not hold null. If we try to assign null to the variable, compiler will throw error.
|
|
To allow a variable to hold null value , we have to declare a variable as nullable string, written String?
|
|
Now, if we want to access the length of the string string1, it guarantees not to throw Null pointer exception, so we can safely say:
|
|
But if we want to access the length of the string string2, that would not be safe, and the compiler reports an error:
|
|
Here, null value can be easily assigned to the variable, thats why the compiler throws error. We should use the null safe operator to access the methods on the variable such as length.
Here comes the rescue, use safe call operator
Safe Call operator(?.)
Kotlin has a Safe call operator, ?. that can be used to execute an action on a variable only when the avriable holds as non-null value. Both null-check and method call can be combined in a single expression using Safe call operator.
|
|
The above expression is equivalent to
|
|
How to use safe operator
|
|