Null Safety in Kotlin is to eliminate the risk of occurrence of NullPointerException in real time. If you are a Java developer, you might have definitely come across the atrocities of NullPointerException it creates during real time. NullPointerException is so popular as NPE (NullPointerException), since it costed billions of dollars to companies. Kotlin is so concerned about this, and made its type system in such a way that it eliminates the usage of NullPointerException, unless you want it explicitly. Thank you Kotlin.

Kotlin could never throw a NullPointerException unless you ask for it.

Of course there are ways you might give NullPointerException a chance to live one more Exception again. Let us see how could this happen :

  • When you ask for a NullPointerException explicitly
    • to throw a NullPointerException or
    • use!! operator. This operator comes with a disclaimer for usage : You have been warned! And yet you chose to live with NullPointerException.
  • From Outside Kotlin
    • You may be aware that you can run external Java code in your application. And this external Java Code is not NullPointerException proof, unless you make it so.
  • Data inconsistency

How Kotlin handles Null Safety ?

Following are some of the ways to handle Null Safety in Kotlin :

Differentiate between nullable and non-nullable references

Kotlin’s type system can differentiate between nullable references and non-nullable references.  ?  operator is used during variable declaration for the differentiation.

To make a variable hold a null value, follow the below example and observe the usage of  ?  in the program

Explicitly check for a null in condition

If condition is an expression in Kotlin and hence can return a value. We may use this to explicitly check for a null and return a value in a single statement.

In the below example, we shall check if the variable ‘str’ is null and access the properties of str if not null.

Safe Call

The safe call operator returns the variables property only if the variable is not null, else it returns null. So the variable holding the return value should be declared nullable. Following is the example program where a safe call is made with the variable b on property length.

Elvis Operator (?:)

If reference to a variable is not null, use the value, else use some default value that is not null. This might sound same as explicitly checking for a null value. But the syntax could be reduced and is given below :

How to get a NullPointerException explicitly ?

Despite the safety measures Kotlin provides to handle NPE, if you need NPE so badly to include in the code, you have it. Following are the ways

Throw an NPE if reference is null using ( !! ) operator

Consider the following example to throw a NullPointerException using !! operator.

Conclusion :

In this Kotlin Tutorial – Kotlin Null Safety, we have learnt what Null Safety is, differences between nullable references and non-nullable references, user explicitly checks for a null in conditions, using a Safe Call Operator (?.) and Elvis Operator (?:).