Null Safety: Non-null Types and Nullability in Kotlin

- Andrés Cruz

En español

Null Safety: Non-null Types and Nullability in Kotlin

One of the problems we have when developing applications in many of the programming languages today, such as C#, Java, and our case of interest, which is developing applications with Android, is the more than annoying Java NULLPointerException or Reference Exception. null that can appear in our application when we least expect it; Kotlin offers several very interesting methods to deal with this annoying scenario and with very little code and in a very easy way.

In this post we will see how to handle non-null data types (that are not allowed to be null), those that can be null, and how to handle what would be a NULLPointerException in Kotlin in Java.

Non-NULLable types

By default, data types in Kotlin are not allowed to be null, which in itself would be a good idea since the compiler will warn us if we ever programmatically (in code) set such variables to null:

var a: String = "abc"

If we try to reference a null value

var a: String = "abc"
a = NULL // error de compilador

The compiler would give us an error; The problem comes now if the value comes from some other source such as input data or from a server provided with a query (in short, at runtime), etc. that can give us a null value for this variable, for these cases and When we want our variable to be null, we must do what is explained in the next section.

Null data types (NULLable types)

To allow nulls in our variables we simply must use the character ? after indicating the specified data type:

var a: String? = "abc"
a = NULL // el compilador lo permite

Of course, with this we return to the initial problem with the null references in the properties and methods that we can use in our defined variables and with this the initial problem in a cycle that does not seem to have an end... or not.

The Kotlin compiler forces us to check if a variable is defined as NULLable or that it can be null before using a property or method of it; that is, if we have the following piece of code:

val x: Int? = NULL

And tried to use some method or property:

val h = x.toDouble()

It would not compile until we check if it is not null:

if (x != NULL) {
    val h = x.toDouble()
}

Unary operators for NULLable types

If we want to be able to use the properties and methods provided by the Kotlin API or generated by third parties or us without being afraid of getting a null reference exception at some point we can use the ? in front of the method property name:

var a: String? = "abc"
a = NULL // bien
a?.length // NULL

In the code snippet above, if we call it at the time of a?.length whose value of the variable a is null, it gives us that a?.length is also null, which would not happen if we did not use the ? before the name of the property to access (?.length).

returning the exception

Is there another operator that we can use similar to ? previously employed, and is the operator !! that being null the queried variable would return an exception:

var a: String? = "abc"
a = NULL // bien
a!!.length // NULLPointerException

In short, the operator ? would return null when accessing a property or method if the variable is null, and the operator !! would return an exception if any property or method of the variable is accessed if it is null.

Returning custom values in null variables

If we want it to return another value that is not null if not some other defined by us, we can also do the following whose scheme is quite similar to short conditionals in PHP:

val l = b?.length ?: -1

Now with this we can use the data types in Kotlin without problems, if we want to use the new Kotlin scheme that does not allow nulls by default, or the most common one that allows nulls but with the problem of null references, we already know how to deal with it. avoid null references when accessing properties or methods; in the next entry we will see how to use the methods and classes in Kotlin.

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 1d 19:03!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!