Skip to content
DeveloperMemos

Kotlin's Null Safety: Preventing NullPointerExceptions

Kotlin, Null Safety, Android Development2 min read

As an Android developer, you might be familiar with the pain of NullPointerExceptions. These errors occur when you try to access a null reference, causing your app to crash. They are common, difficult to debug, and can lead to poor user experiences. Fortunately, Kotlin's Null Safety feature helps prevent these errors from occurring in the first place. In this article, we'll explore what Null Safety is and how it works.

What is Null Safety?

Null Safety is a feature of the Kotlin programming language that aims to eliminate NullPointerExceptions from your code. It does this by providing a type system that distinguishes between nullable and non-nullable types. A nullable type can hold a null value, while a non-nullable type cannot. When you try to access a nullable type, you must explicitly check if it contains a null value or use one of Kotlin's built-in functions to handle null values. This prevents NullPointerExceptions at runtime.

Nullable and Non-Nullable Types

Kotlin's type system includes two types of variables: nullable and non-nullable. By default, all variables in Kotlin are non-nullable, meaning they cannot hold a null value. To make a variable nullable, you must add a question mark after its type declaration. For example:

1var nullableString: String? = "Hello, world!"
2var nonNullableString: String = "Hello, world!"

In this example, nullableString can hold a null value, while nonNullableString cannot. If you try to assign a null value to nonNullableString, the compiler will generate an error.

Safe Calls and Elvis Operator

When working with nullable types, you need to handle null values explicitly. Kotlin provides two ways of doing this: safe calls and the Elvis operator. A safe call is denoted by the question mark, and it returns null if the object being called is null:

1val nullableString: String? = null
2val length = nullableString?.length // length is null

In this example, nullableString?.length returns null because nullableString is null. If nullableString had contained a non-null value, length would have been equal to the length of that value.

The Elvis operator (?:) allows you to provide a default value in case a variable is null:

1val nullableString: String? = null
2val length = nullableString?.length ?: -1 // length is -1

In this example, length is set to -1 because nullableString is null. If nullableString had contained a non-null value, length would have been equal to the length of that value.

Late Initialization

Another feature provided by Kotlin's type system is late initialization. This allows you to declare a non-nullable variable without initializing it immediately. You must initialize the variable before accessing it, but Kotlin ensures that it is not accessed before initialization. For example:

1lateinit var initializedString: String
2
3fun init() {
4 initializedString = "Hello, world!"
5}

In this example, initializedString is declared as a non-nullable variable but is not initialized immediately. The init() function initializes initializedString before it is accessed.

Summary

Kotlin's Null Safety feature helps prevent NullPointerExceptions by providing a type system that distinguishes between nullable and non-nullable types. This forces developers to handle null values explicitly, improving code quality and reducing errors. By using safe calls, the Elvis operator, and late initialization, you can write code that is more robust and less prone to crashes.