Kotlin Null Check

If you have ever written code in Java or another language with the concept of null references, you have probably run across a NullPointerException. A NullPointerException is thrown by the Kotlin compiler if it finds a null reference before executing any additional statements. In the article, we will discuss the preferred and sophisticated technique to effectively manage null safety in Kotlin to use a few specialized operators.

Null and Non-Nullable types in kotlin

The goal of Kotlin is to eliminate the possibility of a NullPointerException. Kotlin’s variables are all non-nullable by default. As a result, we can’t give a variable a null value because it will trigger a compilation error. On a non-nullable variable, we can invoke a function or access a property. However, in the occurrence of nullable variables, we must explicitly handle the null condition. Otherwise, Kotlin will notify that the variable has null references, generating a compilation error.

How to check the null values in kotlin?

To understand the basics of using the technique to check null values in kotlin, we have the following examples:

Example # 1: Using if-else for checking null in Kotlin:

To check conditions, we use the “if” keyword. The “if-else” clause is used in the code to validate a variable’s null safety. Let’s have the code implementation below.

In the above code, we have the main function. The variable is assigned a name “str” and sets the String type property to it. The “str” contains the string value, which we have printed through the kotlin println function. Then, we have an “if” statement in which we have kept the variable “str” not equal to a null value. The “if” block will execute and print the length of the string if the condition is true. Otherwise, we have an “else” block to execute.

As we have a string stored in a variable “str”, the condition becomes true, and the output of the string length is displayed on the screen below.

Example # 2: Using Elvis operator for checking null in Kotlin:

When there is a need to return a default value in the case of a NULL reference, we have to use the Elvis operator. The right-hand side is considered when we have the left-hand side expression null.

In the above code main function, we have declared a variable as “var” and assigned it the name “myStr”. We have set the property of the variable as String type with the null safety operator, which will check the nullability of the variable. The variable contains a string value, and by using the println function, we will print the length of the specified string. Then we have reinitialized a variable “myStr” with the null. In the println function, we have used the Elvis operator “?:” to execute either the length of the string or the left side expression upon condition.

The output “25” is the length of the string which we have initialized in a variable “myStr” at first. After reinitializing the variable “myStr” to null, the output is “-1”. We have used the Elvis operator, of which the left expression executes because the right expression is null.

Example # 3: Using?.let operator for checking null in Kotlin:

We can use the let operator only to act when a reference contains a non-nullable value. If the variable declared isn’t null, the lambda expression inside the “let” will be executed.

In the above code, we have declared the main function in which there is a variable represented with the keyword “var”. The variable is defined as “myList”, with a “List” interface. We have initialized the variable “myList” with some values using the ListOf function. We have created another variable, “newList”, which invokes the listOf function.

Here, we have a “for” loop to which we have assigned the items of “myList” to the “elements”. Then, we have applied the “?.let ” operator on the “elements”, which will only execute for non-nullable values. We have used the plus function in the newList variable, which will return the output of creating a new list from an existing list and a provided item of the newly created List. We have assigned the “newList” items to the “elements” which will be printed using the println function.

Example # 4: Using safety operator for checking null in Kotlin:

If the variable is not null, the null safety operator returns the variable’s property; otherwise, it returns null. As a result, the return value variable should be defined as nullable. The safety operator is denoted with the “?.” sign.
The main function is defined in the code above. We have created two variables in the main function, represented with the keyword “var”. The variables are named “myStr” and “strLen”. The variable “myStr” contains the string value, and the “strLen” is set with the type Int and kept empty. Next, we have “StrLen”, in which the variable “myStr” is used to make a safety null check with the property length. Through the println function, we can see the string and the length of the string as the variables are declared as non-nullable. Then, we have reinitialized the variable “myStr” with null. The safety null check operator is used again with length property in a variable “strLen”. The println function will print the reinitialized variable.

On the terminal screen, the output is presented.


This guide was aimed to brief you about the concept of null check in kotlin in Ubuntu 20.04. We have learned about Null Safety and the distinctions between nullable and non-nullable references. We have gone over a few different approaches to deal with the null check in Kotlin. These examples will be quite effective when working with null checks in Kotlin.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.