Say Goodbye to Crashes by Kotlin (Android)

Arman Kolahan
3 min readMay 26, 2020

Crashes are nightmares of programmers. Change nightmares to dreams by switching from Java to Kotlin in Android development.

Java was for many years the official language of Android development. And the most common reason for crashes was NullPointerException. It was very difficult to handle null safety in java codes until JetBrains come through the Android. By emerging Kotlin, a new language that is designed to interoperate fully with Java, the game changed, step by step. Kotlin has a strong handler for NullPointerException by introducing new syntaxes.

Variable Types (?)

In Kotlin, a variable cannot be null unless you define it as a nullable variable. The question mark (?) is the tool that can manage the nullability of a variable.

In the first case, the following variables cannot be null at all. It means that it should be initialized:

val x: Int = 0
var y: String = "initial"

The bellow examples won't be compiled

val x: Int = null
var y: String

However, in case of needing a nullable variable, it needs to just add the question mark at the end of variable type:

var x: Int? = null
var y: String? = null

In this case the variable can be null, so through the code you need to check null safety.

Safe Call (?.)

An exhausting task in java was checking the safety of a call. For example in java, we had something like this:

Integer x = null
Double y = x.toDouble()

which fails, so should be written like this:

Integer x = null
if( x != null ) {
Double y = x.toDouble()
}

but in Kotlin, it is so simple, by adding the question mark (?):

var x: Int? = null
var y: Double? = x?.toDouble()

In this case, because x is null, so, by null checker (?) y will be null, without any crash due to NullPointerException. Checking null safety in Java it is annoying when the call is nested!

Elvis Operator (?:)

It easily returns a value in case of a var is null by adding :? .

var x: Int? = null
var y: Double = x?.toDouble() ?: 0.0

It is equal to in Java:

Integer x = null
Double y
if( x != null ) {
y = x.toDouble()
} else {
y = 0.0
}

Null Check Avoider (!!)

There is an operator (!!) which avoids null checking. Using this operator is dangerous because in case the variable is null it crashes. For example, the following code will be crash:

var x: Int? = null
var y: Double = x!!.toDouble()

This operator is useful when you are sure that the variable won't be null.

Let (?.let { })

The other useful operator for null checking is let operator. It means to run code when the variable is not null. In the bellow code, the statement inside let won't run, because x is null: (it inside let is equal the variable which is on the left of ?.let .

var x: Int? = null
var y: Double? = null
x?.let {
// here won't run
y = it.toDouble()
}

However, if x won't null the statements inside let will run, same as bellow:

var x: Int? = 0
var y: Double? = null
x?.let {
// here will run
y = it.toDouble()
}

This operator is so useful in handling the null checking of variables.

Except the null safety in Kotlin, there are tons of advantages which convincing to migrate from Java to Kotlin.

--

--

Arman Kolahan

Expert Frontend Developer, Proficient in TypeScript, Kotlin, PHP, and Java, and Additional Experience in Android and Backend.