Kotlin and null safety: Yet Again!

Vinay Joglekar
AndroidPub
Published in
3 min readAug 24, 2019

Interviewer: What are the advantages of using Kotlin to develop android app?

Interviewee: Kotlin is null safe.

Interviewer: Okay. Why?

The above conversation may have happened if you have appeared for the android developer interview in 2018/19.

User user = null;
user.setName("Vinay");

We all know what will happen if the above code is dispatched through the build into the user’s hands. Tony Hoare, had admitted in 2009 that the null reference was his “Billion Dollar Mistake”.

So what do we do here?

User user = null;
if (user != null) {
user.setName("Vinay");
}

Yeah, everyone knows how to add the null check. But real-world apps don’t have only a User object and a LoginViewModel. So what? We add null checks wherever there is a possibility of NPE.

What if there is a better way than adding null checks? What if a language itself does that for us? Let’s have a look!

Nullable Types Vs Non-Nullable Types in Kotlin

val user : User = null

The above Kotlin code will give you a compilation( yeah not at runtime) error: “Null can not be a value of a non-null type User”. Non-Nullable types won’t allow us to assign it’s value to null. To allow the null value, we have to use a different type.

val user : User? = null

In simple terms, User is non-nullable and User? is nullable. This is how the kotlin compiler understands the difference between non-nullable and nullable.

user.name = "Vinay"

The kotlin compiler understands that calling a method from nullable may throw NPE and hence gives a compilation error: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type User?

user?.name = "Vinay"

This is how you null- check in Kotlin, simply by adding “?”.

Alright! But my user has a bunch of properties. Should I add “?” every time while setting a property of the user object? I don’t like that! Fortunately, there is a better way. If you have a block of code depends on a not null condition of a nullable object, simply wrap that block with “?.let

user?.let {
user.name = "Vinay"
user.lastName = "Joglekar"
user.city = "Mumbai"
}

Elvis operator

One of the most common scenarios, when we have a nullable type, is to use the value if it is not null, and default if otherwise.

println(if (user?.city != null) user?.city else "Value is null")

I have made a variable city nullable string. I am printing the value of city if it’s not null else I am printing “Value is null”. Kotlin has provided an Elvis operator for such situations. The Elvis Operator is represented by a question mark followed by a colon as ?:

Using Elvis operator, the above code can simply be written like this :

println(user?.city ?: "Value is null")

The “!!” operator

According to Kotlin official documentation :

The not-null assertion operator (!!) converts any value to a non-null type and throws an exception if the value is null.

user?.city = null
println(user?.city!!)

The above will throw KotlinNullPointerException as the kotlin compiler makes a smart cast from String? to String.

Yes, there is still a possibility of NPE in Kotlin Programming Language.

Use this operator if a variable is of nullable type and you are 100% sure that the variable is not null.

Conclusion

Kotlin provides these many options that include a safe call, ?.let block, Elvis operator and finally assertion operator. Use those carefully and make full use of null safety in your code.

HAPPY CODING!

--

--