Instruction 1

Understanding the Null Data Type

Null is a data type that signifies nothing. It’s not 0, and it’s not an empty set or string. It signifies there’s nothing there at all. It’s non-existent. This isn’t in itself a bad thing. But what happens is that you usually do not design your program thinking that a value would be null. Then, during program execution, some invalid, null data could get set. The null could come from a user, a network issue, or some other unanticipated scenario. Kotlin has near-direct interoperability with Java. Your code can likely access some Java functionality. But Java doesn’t have these null safety features. Java could provide your Kotlin with an unchecked null. This is a major source of nulls in a Kotlin program.

Note: Stating that “Null is a data type that signifies nothing” has no relation with Kotlin Nothing data type.

If you’ve not anticipated null data in your program, you’ll get a NullPointerException. This happens because you try to access a method on the null variable or include it in some operation.

To handle the case where some data is null, you’d check for it and react accordingly. Here’s how it’s done:

fun main() {
   val items: String? = null

   if (items == null){
      println("Invalid data")
   }

    println(items.uppercase())
}

Or you could wrap the expression in a try-catch block, so you don’t have to check for the null value:

fun main() {
   val items: String? = null

   try {
      println(items!!.uppercase())
   }
   catch(e: NullPointerException) {
      println("Invalid data")
   }
}

What do you do then, since every data could be null? You couldn’t possibly wrap every data in a try-catch or check for nulls each time you wanted to use them. Your whole program would look messy if you did that.

That’s why Kotlin has put in measures to combat this issue from the very core of the language itself. It’s called null safety. Null safety in Kotlin involves a series of features that altogether ensure that you don’t ever accidentally try to work with a null data type. Kotlin’s null-handling functionality is priceless!

Kotlin does this by forcing you to be explicit about types that could be null. If they can, it makes sure you can’t directly use it. If you tried, the code wouldn’t compile.

You’d have to handle it safely so it can’t ever result in a NullPointerException.

Initializing A Non-Nullable

Kotlin’s null-safety begins with making a data type nullable. You’ll start with a non-nullable type. You define this type the same as you always do by specifying a non-null value:

fun main() {
   val items = 2
   println(items)
}

Or specifying the type explicitly:

fun main() {
   val items: Int = 2
   println(items)
}

items here is a non-nullable type. If you ever tried to assign a nullable value to it, the code wouldn’t even compile. This is one way that Kotlin’s null-safety features work.

What if you wanted to have a nullable type because you accept null values? Perhaps you accept data from a database that may have some null fields. Or, you access some external API which has nullable fields. Nulls are a part of everyday programming, you’ll come across them as you build software applications.

Initializing A Nullable

To handle nullable values, you have to append ? after the type when declaring the variable:

fun main() {
   val items: Int? = null
   println(items)
}

Run the code and it should print null to the console.

Or, you could simply assign the null to the value if you’re not specifying the type explicitly:

fun main() {
   val items = null
   println(items)
}

Now that you have a nullable type, Kotlin will force you to always handle it safely. You may assign a null or non-null value to a nullable variable. But you’ll always have to use Kotlin’s null-safety features to handle nullables safely. Kotlin offers the null safe operator to handle operations involving nullable values safely.

See forum comments
Download course materials from Github
Previous: Introduction Next: Instruction 2