Null safety is a core feature in Kotlin that helps developers avoid NullPointerException. We can define Nullable and Non-Nullable types in Kotlin and handle Nullable values. This feature is called Kotlin smart null check. In today’s tutorial, we will learn everything about Null safety in Kotlin, including the safe call operator, Elvis operator, and non-null assertions operator.
Kotlin Null Safety: Prevent NullPointerException for Beginners
What is Null Safety in Kotlin?
In Kotlin, null safety helps us avoid unexpected null values. As we have seen many times in our developing career, most app crash issues are caused by NullPointerException. Kotlin allows us to strictly check for Null values so it will not cause the issue later.
Example of Nullalbe and Non-Nullable types in Kotlin:
In Kotlin by default when we define a Type then it is non-nullable. We have to manually make the type nullable to accept its Null values.
1 2 3 4 5 |
// Non-nullable val name: String = "Kotlin Guide" // Nullable type val description: String? = null |
There are 4 ways to handle nullable types in Kotlin:
- Safe Call Operator(?.): The safe call operator is useful when accessing a Nullable type variable. It allows us to call a property or function that might have a null value without causing NullPointerException.
12345678910111213fun main() {val name: String = "Kotlin Guide" // Non-nullableval description: String? = null // Nullable typeval length: Int? = description?.lengthprintln("Description length: $length")}// Output// Description length: null - Elvis Operator(?:): The Elvis operator allows us to handle NullPointerException by providing a fallback value to a variable. If the variable has a null value then it assigns a value.
12345678910111213fun main() {val name: String = "Kotlin Guide" // Non-nullableval description: String? = null // Nullable typeval length = description?.length ?: 0println("Description length: $length")}// Output// Description length: 0 - Non-Null Assertion Operator(!!): The Non-Null assertion operation that the value is not Null. It should be used carefully though it can cause NullPointerException if the value is Null. It is used where we define the If condition to check whether the value is Null or Not.
1234567891011121314151617fun main() {val name: String = "Kotlin Guide" // Non-nullableval description: String? = null // Nullable typeval nonNUllName = name!!.lengthprintln("Name length: $nonNUllName")val nonNullLength = description!!.lengthprintln("Description length: $nonNullLength")}// Output// nonNUllNameName length: 12// Exception in thread "main" java.lang.NullPointerException - Safe Cast(as:): The safe cast operator allows us to safely case an Object to a data type. If the object returns null the casting will be unsuccessful.
123456789101112131415fun main() {val anyValue: Any = "Hello, Kotlin!"// Safe casting to Stringval stringValue: String? = anyValue as? Stringprintln(stringValue)// Trying to cast an incompatible typeval intValue: Int? = anyValue as? Intprintln(intValue)}// Output// Hello, Kotlin!// null
Conclusion:
In Kotlin, the Null safety feature is developed to prevent NullPointerException by encouraging developers to handle Null values. By integrating Safe Call, Elvis operator, Non-Null assertion operator, and Safe casting we can write more optimized code with proper error handling.