Nullable Types in Kotlin
On this page (12sections)
Introduction
Nullable Types is a fundamental concept every Kotlin developer should understand. Null safety is one of Kotlin’s signature features. It catches null-related bugs at compile time instead of crashing at runtime with a NullPointerException.
Kotlin types are divided into nullable and non-nullable types. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.
Definition
- Kotlin types are divided into nullable and non-nullable types.
- A nullable type is declared by adding ? after the type name.
- Non-nullable variables cannot hold null; assigning null causes a compile-time error.
- Nullable types help avoid NullPointerException at runtime.
Syntax
var name: String? = null
var age: Int = 25 // non-nullable
Nullable Types in Kotlin Example Program in Kotlin
fun main(args: Array<String>) {
var city: String? = "Chennai"
println("City: $city")
city = null
println("City after null: $city")
// var country: String = null // compile error
}
Sample Output
City: Chennai
City after null: null
When to use
Use nullable types when a value may legitimately be absent — optional fields, parsed input, or database lookups that can miss.
How it works
-
The program starts with a
mainfunction — the entry point that runs when you execute the file. -
var city: String? = "Chennai"assigns or updates a value used later in the program. -
The
println("City: $city")statement writes a line to the console — this produces part of the sample output below. -
city = nullassigns or updates a value used later in the program. -
The
println("City after null: $city")statement writes a line to the console — this produces part of the sample output below. -
Comment:
var country: String = null // compile error— documents intent for readers. -
Kotlin types are divided into nullable and non-nullable types.
-
Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (
kotlinc/kotlin). Compare your console output with the sample output shown below.
Best Practices
- Prefer non-nullable types by default; add
?only when null is a valid state. - Use safe call (
?.) and Elvis (?:) instead of force unwrap (!!) unless you are certain. - Handle nullable collections and nested properties with
?.chains orletblocks.
Common Mistakes
- Using
!!everywhere instead of handling null safely — this reintroduces NPE risk. - Forgetting that platform types from Java are implicitly nullable.
- Comparing nullable strings with
==without considering both sides may be null.
Key Points
- Kotlin types are divided into nullable and non-nullable types.
- A nullable type is declared by adding ? after the type name.
- Non-nullable variables cannot hold null; assigning null causes a compile-time error.
- Nullable types help avoid NullPointerException at runtime.
- Test the example locally and verify the output matches the sample.
- Experiment by changing input values to see how behaviour changes.
Notes
- Semicolons at the end of statements are optional in Kotlin.