How to Perform Null Safety Checks In Kotlin?

10 minutes read

In Kotlin, null safety is an essential feature that helps prevent null pointer exceptions. To perform null safety checks, you can use the safe call operator (?.), the not-null assertion operator (!!), or the safe cast operator (as?).

  1. Safe Call Operator (?.): The safe call operator lets you execute a statement or access a property only if the object reference is not null. If the object is null, the expression evaluates to null instead of throwing a null pointer exception. Here's an example:
1
2
val name: String? = null
val length: Int? = name?.length


In the above code, name?.length will be null because name is null. If name had a non-null value, the length of the string would be assigned to length.

  1. Not-null Assertion Operator (!!): The not-null assertion operator is used to explicitly tell the compiler that an expression is never null. It converts any value to a non-null type. However, if the expression is null, a NullPointerException is thrown. Here's an example:
1
2
val name: String? = null
val length: Int = name!!.length


In this case, name!!.length will throw a NullPointerException because we're asserting that name is not null, but it is.

  1. Safe Cast Operator (as?): The safe cast operator allows you to safely cast an object to a specific type and get the result as either the target type or null if the casting fails. It eliminates the need for manual type checks. Here's an example:
1
2
val obj: Any? = "Hello"
val length: Int? = obj as? String?.length


In this example, obj as? String?.length will be null because obj is not of type String.


By utilizing these null safety operators, you can ensure that your code handles null values gracefully and avoids null pointer exceptions.

Best Kotlin Books to Read of December 2024

1
Atomic Kotlin

Rating is 5 out of 5

Atomic Kotlin

2
Kotlin in Action

Rating is 4.9 out of 5

Kotlin in Action

3
Kotlin Cookbook: A Problem-Focused Approach

Rating is 4.8 out of 5

Kotlin Cookbook: A Problem-Focused Approach

4
Head First Kotlin: A Brain-Friendly Guide

Rating is 4.7 out of 5

Head First Kotlin: A Brain-Friendly Guide

5
Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

Rating is 4.6 out of 5

Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

6
Effective Kotlin: Best Practices (Kotlin for Developers Book 5)

Rating is 4.5 out of 5

Effective Kotlin: Best Practices (Kotlin for Developers Book 5)

7
Java to Kotlin: A Refactoring Guidebook

Rating is 4.4 out of 5

Java to Kotlin: A Refactoring Guidebook

8
Learn to Program with Kotlin: From the Basics to Projects with Text and Image Processing

Rating is 4.3 out of 5

Learn to Program with Kotlin: From the Basics to Projects with Text and Image Processing


What is the difference between let and run functions for null safety in Kotlin?

In Kotlin, the let and run functions are both used for null safety and enhancing the readability and conciseness of code. However, they have some differences in terms of their functionalities:

  1. let function: The let function can be used to perform operations on nullable objects. It transforms the object if it's not null and returns the result. If the object is null, it simply returns null. The syntax of the let function is as follows:
1
nullableObject?.let { /* operations to be performed on the object */ }


Example:

1
2
val name: String? = "John"
val length = name?.let { it.length } // If name is not null, the length of the name is assigned to the 'length', otherwise, 'length' will be null.


  1. run function: The run function can be used for both nullable and non-nullable objects. It executes a block of code on the object (which can be referred using 'this') and returns the result. If the object is null, an exception may be thrown or a default value can be returned. The syntax of the run function is as follows:
1
nullableObject?.run { /* operations to be performed on the object */ }


Example:

1
2
val name: String? = "John"
val length = name?.run { length } // If name is not null, the length of the name is assigned to the 'length', otherwise, 'length' will be null.


In summary, the let function is primarily used for nullable objects, allowing you to perform operations on them within a safe context, while the run function can be used for both nullable and non-nullable objects, enabling you to execute code on the object itself.


How to use the safe call operator in Kotlin?

In Kotlin, the safe call operator "?." is used to safely access properties or call functions on nullable objects without causing a NullPointerException. Here is how to use the safe call operator in different scenarios:

  1. Accessing nullable properties: If you have a nullable object, you can use the safe call operator to access its properties without explicitly checking for null. If the object is null, the expression will return null instead of throwing an exception. Example: val name: String? = null val length = name?.length // length will be null if name is null, otherwise it will be the length of the name
  2. Chaining property access: You can chain multiple property accesses using the safe call operator. If any intermediate property in the chain is null, the expression will return null without throwing a NullPointerException. Example: val person: Person? = getPerson() val city: String? = person?.address?.city // city will be null if person or address is null, otherwise it will be the city of the person's address
  3. Calling nullable functions: You can use the safe call operator to call functions on nullable objects. If the object is null, the function call will be skipped and the expression will return null. Example: val person: Person? = getPerson() val name: String? = person?.getName() // name will be null if person is null or getName() returns null, otherwise it will be the name of the person
  4. Combining with the Elvis operator: You can use the Elvis operator "?:" in combination with the safe call operator to provide a default value in case of null. Example: val person: Person? = getPerson() val name: String = person?.name ?: "Unknown" // name will be the person's name if person is not null, otherwise it will be "Unknown"


Note that the safe call operator returns null if the object itself is null, but it can still be used on non-null objects as a precautionary measure to handle potential null values in properties or function calls.


How to use the assert function for null safety in Kotlin?

In Kotlin, the assert function can be used to enforce null safety. The assert function throws an AssertionError if the given expression evaluates to false. Here's how you can use it for null safety:

  1. For nullable types, you can use the assertNotNull function to ensure that a value is not null. If the value is null, an AssertionError is thrown.
1
2
val nullableValue: String? = null
assertNotNull(nullableValue) // Throws AssertionError


  1. When you want to assert that a value is null, you can use the assertNull function. If the value is not null, an AssertionError is thrown.
1
2
val nullableValue: String? = "Hello"
assertNull(nullableValue) // Throws AssertionError


  1. You can also use the assert function with an assertion expression to provide your own custom assertion logic for null safety. In this case, the expression should evaluate to true for the assertion to pass.
1
2
val nullableValue: String? = null
assert(nullableValue != null && nullableValue.length > 5) // Throws AssertionError


Note that assertions are enabled only in debug builds by default. If you want to enforce null safety in release builds as well, you need to enable assertions explicitly by adding the "-ea" JVM option.

Twitter LinkedIn Telegram Whatsapp

Related Posts:

In Kotlin, the "let" extension function is used to operate on nullable objects and safely perform operations on them within a code block. It works by passing the object reference as a parameter to the function block and executing the operations on the ...
In SPARQL, when querying a dataset, it is possible to receive null results for specific variables in the results. This can happen when the query matches certain patterns in the dataset, but does not find any values for the variables being selected.To return nu...
To create a Kotlin UInt from Java, you can use the following code snippets:In Java: import kotlin.jvm.JvmField; public class JavaClass { @JvmField public static int createUInt() { return 10; } } In Kotlin: val uintValue = JavaClass.createU...