In this tutorial, we shall learn how to handle Kotlin Compilation Error Variable must be initialized. Usually this compilation error occurs when we try to access a variable (or its methods) which is declared but not initialized.

We shall look into an example where we can reproduce this compilation error.

In the below example, we have declared variable “users” of type ArrayList but not initialized. When we try to add an element to the list, it throws compilation error saying that the variable is not initialized, which is true. We have only declared, but not initialized the variable “users”.

Fix Kotlin Compilation Error Variable must be initialized

We shall try initializing the two variables “name” and “users” and run the program once again.

Voila! In this Kotlin tutorial we have handled “Error Variable must be initialized” by initializing the variable.