Horje

Kotlin - Null Safety
Start
  Smart casts

If the compiler can infer that an object can't be null at a certain point, you don't have to use the special operators anymore:

var string: String? = "Hello!"
print(string.length)  // Compile error
if(string != null) {
    // The compiler now knows that string can't be null
    print(string.length)  // It works now!
}

Note: The compiler won't allow you to smart cast mutable variables that could potentially be modified between the null-check and the intended usage. If a variable is accessible from outside the scope of the current block (because they are members of a non-local object, for example), you need to create a new, local reference which you can then smart cast and use.

  Assertion

!! suffixes ignore nullability and returns a non-null version of that type. KotlinNullPointerException will be thrown if the object is a null.

val message: String? = null
println(message!!) //KotlinNullPointerException thrown, app crashes

  Nullable and Non-Nullable types

Normal types, like String, are not nullable. To make them able to hold null values, you have to explicitly denote that by putting a ? behind them: String?

  Eliminate nulls from an Iterable and array

Sometimes we need to change type from Collection<T?> to Collections<T>. In that case, filterNotNull is our solution.

val a: List<Int?> = listOf(1, 2, 3, null)
val b: List<Int> = a.filterNotNull()

  Null Coalescing / Elvis Operator

Sometimes it is desirable to evaluate a nullable expression in an if-else fashion. The elvis operator, ?:, can be used in Kotlin for such a situation.

For instance:

val value: String = data?.first() ?: "Nothing here."

The expression above returns "Nothing here" if data?.first() or data itself yield a null value else the result of data?.first().

It is also possible to throw exceptions using the same syntax to abort code execution.

val value: String = data?.second() 
    ?: throw IllegalArgumentException("Value can't be null!")

Reminder: NullPointerExceptions can be thrown using the assertion operator (opens new window) (e.g. data!!.second()!!)

  Safe call operator

To access functions and properties of nullable types, you have to use special operators.

The first one, ?., gives you the property or function you're trying to access, or it gives you null if the object is null:

apply</code></a> like this:</p>

```kotlin
obj?.apply { 
    foo()
    bar()
}

This will call foo and bar on obj (which is this in the apply block) only if obj is non-null, skipping the entire block otherwise.

To bring a nullable variable into scope as a non-nullable reference without making it the implicit receiver of function and property calls, you can use let (opens new window) instead of apply:

nullable?.let { notnull ->
    notnull.foo()
    notnull.bar()
}

notnull could be named anything, or even left out and used through the implicit lambda parameter it. (opens new window)

  Null Coalescing / Elvis Operator

Sometimes it is desirable to evaluate a nullable expression in an if-else fashion. The elvis operator, ?:, can be used in Kotlin for such a situation.

For instance:

val value: String = data?.first() ?: "Nothing here."

The expression above returns "Nothing here" if data?.first() or data itself yield a null value else the result of data?.first().

It is also possible to throw exceptions using the same syntax to abort code execution.

val value: String = data?.second() 
    ?: throw IllegalArgumentException("Value can't be null!")

Reminder: NullPointerExceptions can be thrown using the assertion operator (opens new window) (e.g. data!!.second()!!)


Read Article
https://develop.horje.com/learn/1434/reference