Null Safety

If there’s one thing that causes a lot of fuzz amongst the Kotlin adopters it is the Null Safety feature of the language.

This is understandable since in several programming languages null values are a common cause of errors and crashes.

But what does that mean?

It means that sometimes, you call an element and ask it to do something but there’s no value assigned to it, it’s null, it does not exist. And a non-existing thing can’t do anything, so the app returns an error, basically saying: “Hey, you have requested this String to be passed as a parameter of this function, but that String is unknown/ non-existent and I don’t know what to do, so I’m gonna throw an error, sorry (not sorry) BAM!”

And Kotlin has a bunch of features that force developers to think about the possibilities of null values in their codes which helps avoid those errors and crashes mentioned above.

What is nullability?

To start, let’s establish that null is not the same as zero. Null is the equivalent of empty. So, let’s say you have a val Score: Int that will hold the score of a game. The initial score has a value of 0 (zero), score = 0 which has a value of zero assigned to it. Null would be if not even the value of zero had been assigned to the variable. It’s simply empty and when you call score it has nothing to give back to you so it causes an error.

It’s worth noting that Null is also different from a blank space string like
val message = " " or even an empty string like val message = "" (no blank space).

So, if you think of an actual electronic board, val scoreHome = 0 and
val scoreVisitor = 0 will show like this:

While val scoreHome = null would show an empty scoreboard and you would not be able to perform any actions on it, like incrementing the score, for example, until it gets an actual integer value assigned to it.
So, currentScoreHome = scoreHome++ is not possible since scoreHome = null

The thing is, you might want to have a null value assigned to a variable and it’s important to know how to work with a null value if you need to use it.

When do I need to use null?

For example, sometimes you won’t know the value that will be assigned to a variable. Especially if you are dealing with external sources, meaning, if your app is not a closed environment and relies only on data provided within your codebase. So, if you are retrieving data via API you have no control over what kind of information your app will pull, you might want to prepare in case it returns no data at all.

But you need to be careful when using a null value.

As I mentioned above, let’s say you have this code:

Int score = null;

This is totally permitted in Java but if you do something like this:

Int newScore = score++;

You will get an error. This code will cause an exception that will crash the program, called a NullPointerException because you are requesting a variable that has a null value to be incremented. So, it’s asking a non-existing thing to do something and it can’t. 🤷‍♀️

In Kotlin, the same code would show a squiggly red line under the null value and suggest you should use a null-safe type that can handle both the original type value, an Int in this case, or a null value if you are really sure this can be null and tells you to change Int to Int?

Kotlin verifies all your assignments to make sure you don’t accidentally assign null values to variables that are not marked as nullable.

It handles the problem at compile-time instead of crashing the app at run time.

It seems like a simple thing, but it actually prevents a lot of issues.

Published by Rosie M

Android bug creator and full-time nerd. Joined Automattic in 2017. Passionate about music, books, games, photography and Doctor Who. Open-source enthusiast, remote-work advocate and Globetrotter.

2 thoughts on “Null Safety

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: