Introduction to Nullability

Explore Kotlin's solutions to nullability issues, including explicit rules and versatile tools for efficient null management.

Understanding the need for Kotlin

Kotlin started as a remedy to Java problems, and one of the biggest problems in Java is nullability. In Java and many other languages, every variable is nullable, so it might have the null value. Every call on a null value leads to the famous NullPointerException (NPEIt is a runtime error that occurs when a program tries to use or access an object that doesn’t exist (is null).). This is the number one exception in most Java projects. It is so common that it is often referred to as the billion-dollar mistake after the famous speech by Sir Charles Antony Richard Hoare, where he said:

I call it my billion-dollar mistake. It was the invention of the null reference in 1965… This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

Kotlin’s solution

One of Kotlin’s priorities was to finally solve this problem, and it achieved this perfectly. The mechanisms introduced in Kotlin are so effective that seeing NullPointerException thrown from Kotlin code is extremely rare. The null value stopped being a problem, and Kotlin developers are no longer scared of it. It has become our friend.

Nullability rules

So, how does nullability work in Kotlin? Everything is based on a few rules:

  1. Every property needs to have an explicit value. There is no such thing as an implicit null value.

Get hands-on with 1400+ tech skills courses.