Saturday 17 August 2019

Kotlin Null Handling


- Kotlin Null Safe
- Here we can’t initialise or assign null value.
- If purposely we need to have null, it provides with Question mark.

val username: String? = null

Here we have 4 types of null handling shots.

1. Safe Call
2. Safe Call with let
3. Elvis Operator
4. Non-Null Assertion Operator.

1. Safe Call: It is represented by "?." and this facilitates to access the methods of the object.  In case the reference object or variable is null it will safely handle that and produce the return value as string as "null"

var username: String? = null
println("Username Length is : ${username?.length}")   // We won't get crash, it just print the value as null
username = "manakular"
println("Username Length is : ${username?.length}")

Output:


Username Length is : null
Username Length is : 9

2. Safe Call with let:  It is also similar to the Safe call, instead of accessing the method this allows you to execute the block of code and implicitly returns the last line of the block based on the code,  here "it" keyword will refer the concern data value.

    var username: String? = "manakularpondy"
    val status: String? = username?.let {
        println("Username is $it")
        if (it.length > 10) "Valid Input" else "Invalid length"
    }
    println(status)

Output:

Username is manakularpondy
Valid Input

In case, if we change the value to null  var username: String? = null  the status value will be also null.  So, in order to handle the failure i.e. null scenario we can take help of run block.


    var username: String? = null
    val status: String? = username?.let {
        println("Username is $it")
        if (it.length > 10) "Valid Input" else "Invalid length"
    }.run {
        "Need Input"
    }
    println(status)

Output:

Need Input

3. Elvis Operator: Hope we most of them aware we don't have ternary operator in kotlin programming, but similar to that we are having this elvis operator.  This will be denoted by ?: as mentioned on the below code snippet the username is initialised as null and once we trying to access the length of the variable we are all aware it going to be a null type.   Hence so we are accessing the username value via Safe call (?), then it sees the value as null our elvis operator helps to executes the or take the values after the ?:  Hence here we can see the length value as  0 even the username value is null.    

var username: String? =null
val length: Int = username?.length ?: 0
println("Length is $length")

Output:

Length is 0

4. Non-Null Assertion Operator: This need to handled very carefully, it is referred by using the double exclamation (!!).   Since you are 100% sure the object is non-null then you can use this else the value is null it will throws the null pointer exception.


var username: String? =null
val length: Int = username!!.length ?: 0
println("Length is $length")

The above code snippet throws the null pointer exception, because username object is null

No comments: