Kotlin Help

Nullability in Java and Kotlin

Nullability is the ability of a variable to hold a null value. When a variable contains null, an attempt to dereference the variable leads to a NullPointerException. There are many ways to write code in order to minimize the probability of receiving null pointer exceptions.

This guide covers differences between Java's and Kotlin's approaches to handling possibly nullable variables. It will help you migrate from Java to Kotlin and write your code in authentic Kotlin style.

The first part of this guide covers the most important difference – support for nullable types in Kotlin and how Kotlin processes types from Java code. The second part, starting from Checking the result of a function call, examines several specific cases to explain certain differences.

Learn more about null safety in Kotlin.

Support for nullable types

The most important difference between Kotlin's and Java's type systems is Kotlin's explicit support for nullable types. It is a way to indicate which variables can possibly hold a null value. If a variable can be null, it's not safe to call a method on the variable because this can cause a NullPointerException. Kotlin prohibits such calls at compile time and thereby prevents lots of possible exceptions. At runtime, objects of nullable types and objects of non-nullable types are treated the same: A nullable type isn't a wrapper for a non-nullable type. All checks are performed at compile time. That means there's almost no runtime overhead for working with nullable types in Kotlin.

In Java, if you don't write null checks, methods may throw a NullPointerException:

// Java int stringLength(String a) { return a.length(); } void main() { stringLength(null); // Throws a `NullPointerException` }

This call will have the following output:

java.lang.NullPointerException: Cannot invoke "String.length()" because "a" is null at test.java.Nullability.stringLength(Nullability.java:8) at test.java.Nullability.main(Nullability.java:12) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

In Kotlin, all regular types are non-nullable by default unless you explicitly mark them as nullable. If you don't expect a to be null, declare the stringLength() function as follows:

// Kotlin fun stringLength(a: String) = a.length

The parameter a has the String type, which in Kotlin means it must always contain a String instance and it cannot contain null. Nullable types in Kotlin are marked with a question mark ?, for example, String?. The situation with a NullPointerException at runtime is impossible if a is String because the compiler enforces the rule that all arguments of stringLength() not be null.

An attempt to pass a null value to the stringLength(a: String) function will result in a compile-time error, "Null can not be a value of a non-null type String":

Passing null to a non-nullable function error

If you want to use this function with any arguments, including null, use a question mark after the argument type String? and check inside the function body to ensure that the value of the argument is not null:

// Kotlin fun stringLength(a: String?): Int = if (a != null) a.length else 0

After the check is passed successfully, the compiler treats the variable as if it were of the non-nullable type String in the scope where the compiler performs the check.

If you don't perform this check, the code will fail to compile with the following message: "Only safe (?.) or non-nullable asserted (!!.) calls are allowed on a nullable receiver of type String?".

You can write the same shorter – use the safe-call operator ?. (If-not-null shorthand), which allows you to combine a null check and a method call into a single operation:

// Kotlin fun stringLength(a: String?): Int = a?.length ?: 0

Platform types

In Java, you can use annotations showing whether a variable can or cannot be null. Such annotations aren't part of the standard library, but you can add them separately. For example, you can use the JetBrains annotations @Nullable and @NotNull (from the org.jetbrains.annotations package) or annotations from Eclipse (org.eclipse.jdt.annotation). Kotlin can recognize such annotations when you're calling Java code from Kotlin code and will treat types according to their annotations.

If your Java code doesn't have these annotations, then Kotlin will treat Java types as platform types. But since Kotlin doesn't have nullability information for such types, its compiler will allow all operations on them. You will need to decide whether to perform null checks, because:

  • Just as in Java, you'll get a NullPointerException if you try to perform an operation on null.

  • The compiler won't highlight any redundant null checks, which it normally does when you perform a null-safe operation on a value of a non-nullable type.

Learn more about calling Java from Kotlin in regard to null-safety and platform types.

Support for definitely non-nullable types

In Kotlin, if you want to override a Java method that contains @NotNull as an argument, you need Kotlin's definitely non-nullable types.

For example, consider this load() method in Java:

import org.jetbrains.annotations.*; public interface Game<T> { public T save(T x) {} @NotNull public T load(@NotNull T x) {} }

To override the load() method in Kotlin successfully, you need T1 to be declared as definitely non-nullable (T1 & Any):

interface ArcadeGame<T1> : Game<T1> { override fun save(x: T1): T1 // T1 is definitely non-nullable override fun load(x: T1 & Any): T1 & Any }

Learn more about generic types that are definitely non-nullable.

Checking the result of a function call

One of the most common situations where you need to check for null is when you obtain a result from a function call.

In the following example, there are two classes, Order and Customer. Order has a reference to an instance of Customer. The findOrder() function returns an instance of the Order class, or null if it can't find the order. The objective is to process the customer instance of the retrieved order.

Here are the classes in Java:

//Java record Order (Customer customer) {} record Customer (String name) {}

In Java, call the function and do an if-not-null check on the result to proceed with the dereferencing of the required property:

// Java Order order = findOrder(); if (order != null) { processCustomer(order.getCustomer()); }

Converting the Java code above to Kotlin code directly results in the following:

// Kotlin data class Order(val customer: Customer) data class Customer(val name: String) val order = findOrder() // Direct conversion if (order != null){ processCustomer(order.customer) }

Use the safe-call operator ?. (If-not-null shorthand) in combination with any of the scope functions from the standard library. The let function is usually used for this:

// Kotlin val order = findOrder() order?.let { processCustomer(it.customer) }

Here is a shorter version of the same:

// Kotlin findOrder()?.customer?.let(::processCustomer)

Default values instead of null

Checking for null is often used in combination with setting the default value in case the null check is successful.

The Java code with a null check:

// Java Order order = findOrder(); if (order == null) { order = new Order(new Customer("Antonio")) }

To express the same in Kotlin, use the Elvis operator (If-not-null-else shorthand):

// Kotlin val order = findOrder() ?: Order(Customer("Antonio"))

Functions returning a value or null

In Java, you need to be careful when working with list elements. You should always check whether an element exists at an index before you attempt to use the element:

// Java var numbers = new ArrayList<Integer>(); numbers.add(1); numbers.add(2); System.out.println(numbers.get(0)); //numbers.get(5) // Exception!

The Kotlin standard library often provides functions whose names indicate whether they can possibly return a null value. This is especially common in the collections API:

fun main() { //sampleStart // Kotlin // The same code as in Java: val numbers = listOf(1, 2) println(numbers[0]) // Can throw IndexOutOfBoundsException if the collection is empty //numbers.get(5) // Exception! // More abilities: println(numbers.firstOrNull()) println(numbers.getOrNull(5)) // null //sampleEnd }

Aggregate operations

When you need to get the biggest element or null if there are no elements, in Java you would use the Stream API:

// Java var numbers = new ArrayList<Integer>(); var max = numbers.stream().max(Comparator.naturalOrder()).orElse(null); System.out.println("Max: " + max);

In Kotlin, use aggregate operations:

// Kotlin val numbers = listOf<Int>() println("Max: ${numbers.maxOrNull()}")

Learn more about collections in Java and Kotlin.

Casting types safely

When you need to safely cast a type, in Java you would use the instanceof operator and then check how well it worked:

// Java int getStringLength(Object y) { return y instanceof String x ? x.length() : -1; } void main() { System.out.println(getStringLength(1)); // Prints `-1` }

To avoid exceptions in Kotlin, use the safe cast operator as?, which returns null on failure:

// Kotlin fun main() { println(getStringLength(1)) // Prints `-1` } fun getStringLength(y: Any): Int { val x: String? = y as? String // null return x?.length ?: -1 // Returns -1 because `x` is null }

What's next?

If you have a favorite idiom, feel free to share it with us by sending a pull request!

Last modified: 11 September 2023