Sling Academy
Home/Kotlin/Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin

Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin

Last updated: December 05, 2024

One of the common issues faced by developers, especially those coming from languages like Java, is NullPointerException, often affectionately referred to as the "billion-dollar mistake." Kotlin provides several mechanisms to work with null values gracefully and safely. One such tool is the safe call operator ?..

Understanding the Safe Call Operator

The safe call operator ?. in Kotlin allows you to access properties or call methods on objects without worrying about null references that could lead to a NullPointerException. When you use ?., if the object is null, the call will return null rather than throwing an exception. This is a concise way of handling potential nullability in your variable interactions.

Basic Syntax


val length: Int? = string?.length

In this short example, if string is not null, length will store the length of the string. If string is null, length will be assigned null instead of attempting to access properties on a null object, thus preventing a crash.

Real-world Applications

Now let’s consider some practical scenarios where using the safe call operator can be particularly beneficial:

Handling Nested Nullability

Consider handling deeply nested objects which might have multiple nullable layers:


data class Person(val name: String, val car: Car?)
data class Car(val brand: String, val insurance: Insurance?)
data class Insurance(val company: String)

val person: Person? = // some initialized value
val companyName: String? = person?.car?.insurance?.company

In this snippet, access to insurance company in a nullable and nested context is simplified. If any objects in the chain are null, the expression will return null instantly without throwing a NullPointerException.

Function Calls

Consider an example where you need to call a method on a nullable object:


fun printName(person: Person?) {
    println(person?.name)
}

Here, the safe call operator is used to print the person's name if available. If the person is null, the method handles it gracefully by returning null and thus printing "null" without any crash, which might otherwise require verbose null-checking.

Combining with Elvis Operator

The safe call can be combined effectively with the Elvis operator ?: to provide default values quickly:


val result = person?.car?.insurance?.company ?: "Unknown"

Here, if the chain returns null, "Unknown" is used as a default value, making it both an efficient and expressive way to handle potentially null values.

Side Effects

Note that safe calls are only useful in specific situations where the object can indeed be null. For performance reasons, avoid overusing it when unnecessary, as accessing properties or calling methods on non-nullable references doesn't require it.

Conclusion

Kotlin's safe call operator ?. provides an elegant and concise solution to the perennial problem of NullPointerException. It becomes even more powerful when paired with Kotlin's other features, such as the Elvis operator or assertions. Its ability to simplify complex null-checking code means more readable and maintainable code, which is always a win for any developer.

Next Article: Chaining Safe Calls for Complex Operations in Kotlin

Previous Article: How to Declare Nullable Variables in Kotlin

Series: Null Safety in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin
  • Combining Safe Calls with Collections in Kotlin