Safely accessing lateinit properties in Kotlin

/ Development Tips / 2 min read

Kotlin, by design, doesn't allow a non-null variable to be left uninitialized during it's declaration.

If you've been digging into Kotlin you'll know that a lateinit property allows you to overcome this hurdle.

However, if you're a complete newbie, lateinit allows you to declare a variable first and then initialize is somepoint in the future during your app's execution cycle.

All these seem rosy, so where's the problem?

The problem occurs when you try to access an uninitialized property. Let's talk more about that.

Accessing an uninitialized property

Whenever you declare a lateinit var, you need to initialize it before you access it. Otherwise, you'll be greeted with a fancy exception like this:

Exception in thread "main" kotlin.UninitializedPropertyAccessException: lateinit property fullName has not been initialized
	at UninitializedPropertyKt.main(UninitializedProperty.kt:3)

Don't mistake this for a puny exception. It'll crash your app.

And this is a very common situation to run into. Take this as an example:

You might have a list data being fetched from a remote server and need to initialize a RecyclerView adapter based on that data.

What happens when you try to access that adapter before you actually get the data from the remote server?

Boom! Your app crashes.

So, how to solve this problem?

Taking the rookie approach

The most lucrative solution to this problem would be to make the property a regular nullable one instead of a lateinit var and assign a value later on.

You can do something like this:

var fullName: String? = null

And then just do a plain null check whenever you're accessing the value.

if (fullName != null) {
    print("Hi, $fullName")

Kind of like Java. But hang on a sec, Kotlin is supposed to be better than Java. Also, one of the USPs of Kotlin was eliminating the fiasco caused by a NullPointerException.

So, why go the traditional route?

Here's a better solution.

Going the Kotlinish way

If you're using Kotlin 1.2, you can easily check whether a lateinit variable has been initialized or not. If not, well, you can always use the not null approach.

Anyways, here's how you can check if a lateinit var has been initialized or not:

if (::fullName.isInitialized) {
    print("Hi, $fullName")

According to the official blog post announcing this update, this approach uses reflection to check whether the value has been initialized or not.

Also, a deinitialize method is due for rollout in a future release, probably in Kotlin 1.3.

What's the motivation here?

When I first encountered the exception mentioned in this article, I had two choices:

  • Go the traditional not null way
  • Do some cool shit

I took the latter approach.

I didn't want to litter my code with null checks. Also, this looks more meaningful.

Traditional is, well, traditional. Go along with the new.