muriel gonzalez

web and mobile developer


Null Safety in Kotlin

As an android dev, it is very enjoyable working with Kotlin’s type system. With Kotlin's nullability support, it becomes much easier to avoid the NullPointerException hell that is so common when developing in Java.

In this post, I will cover Kotlin's awesome built in null safety features and give some examples on how to use them.

In Kotlin, we declare a nullable type with a question mark ?:

val item: MediaItem? = null
item.print() // ❌ this won't compile 👎

When accessing the property of a nullable variable, the compiler's type checker will report an error and the code just won't compile.

In order to properly test and work with nullable types, we have some options:

1. Check for null

This is the same approach as we do so often in Java

if (item != null) {
	item.print() // ✅ this is ok👌

2. Safe Call operator ?.

Safe calls are very useful when chaining different calls together, or on the left side of an assignment.

val item: MediaItem? = null
item?.print() // ✅

3. The elvis operator ?:

We can assign a non-null value when our reference is of nullable type:

val myInt: Int? = null
// give alternative value in case myInt is null
val myLong: Long = myInt?.toLong() ?: 0L

// this is the equivalent to expression above
val myLong2 : Long = if (myInt != null) myInt else 0L

4. The dreaded !! operator.

⚠️ This not-null assertion operator is really dangerous, so only use it when…


// 😡😡😡
val item: MediaItem? = null
item!!print() // 😾 will throw exception if value is null

Lastly, using ?. allows us to throw different types of exceptions, however, !! will only be able to throw a NullPointerException

You can learn more about null safety in Kotlin here Null safety - Kotlin Programming Language

©2022 Muriel Gonzalez | Built with Gatsby