FREELessons: 20Length: 1.7 hours

• Overview
• Transcript

# 3.1 Goodbye Null Pointer Exception

In this lesson, I'll focus in on one of the most important features of Kotlin: null safety. I'll show you how Kotlin improves handling of null values and how to use the Kotlin operators to handle nulls and avoid null pointer exceptions.

## 5.Conclusion1 lesson, 01:17

### 3.1 Goodbye Null Pointer Exception

Hey, there, in this lesson we will be learning about one of the most important key points of Kotlin, that is the nullability. How Kotlin handles nullability? How it is better than Java when it comes to the safety? Without wasting further time, let's jump into it. Basically, Kotlin is designed to eliminate the NullPointerException. The only reason why we may have NullPointerExceptions in Kotlin may be an explicit call to the NullPointerException function. There may be some external Java code that may cause the NullPointerException. Or there may be some inconsistency when it comes to initialization. For example an uninitialized, this may be there somewhere in the constructor in some classes. This will cause the NullPointerException or the NullPointerException. With the use of the double exclamation operator which we will be learning in a few. Let's see a few examples by defining some variables and initializing [INAUDIBLE] and see how Kotlin handles it. We have a variable element which is is of string type and has the value of triangle. Since it is mutable, we can reinitialize element. You see we get an error here. It says, null cannot be a value of a non-null type string. By default, the variables are not allowed to hold the null values. So what do we do if we want a variable to be null? To do that, let us take another example. And change this element to figure. And here, we want to allow figure to carry the null value. If we want the variable to have the null value, we need to append the question mark operator to the data type. If we want the figure to be of integer type, let's say 1, we will put Int question mark 1. I'll be using the string here and defining it to circle and then the figure can be deinitialized to null. Now let's say we want to find out the length of the string element. But again if we want to find out and print the length of the figure variable. But since figure is null here, we will get an exception. Let us first print out the good code, and see what happens. We have the output as triangle length is equal to 8, now if we print figure dot length, we get an error here, it says, only save or non-null asserted calls are allowed on a nullable receiver of type string question mark. So here we need to first make sure that the variable is an ordinal and then we need to implement the functions on the string. To do this, we need to check for the null conditions. Using the traditional method, what we will do is we would find out if this variable was null, if it was not null we will print the length of the variable. We would do this something like this. But this is not the Kotlin way of doing it. We have an easier method which Kotlin has given us. We will live the safe goals. Now what is safe goal? The syntax to use for the safe goal is println(figure.length). We have defined figure as an string. So here we will put the question mark and find the length. This means that, if figure is not null, find out the length. So instead of writing these three lines of code, this just is completed in a single line. This is what is the beauty of Kotlin. It is very concise and very easy to understand. Reducing the length of the code by a tremendous amount. Let us see what this line of code written says figure isn't real. Aha! Here we have the null. So triangle length is equal to eight comes from this line. And since figure is null, we have the value null. Let's see if figure were not null, what would have been the value for this code. You see here is the difference, this is six, the length of the figure variable which has the value of circle is six. Hence we can conclude that, there is the safe cause for the null type variables in Kotlin. I had mention earlier the use of double exclamation operator. If you love null pointer exceptions you can have it. Kotlin won't let you not have it. Let's say if we use the double exclamation operator to find out the length of the variable figure, what happens? Here we have the use of a double exclamation operator. This is just opposite of the safe call operator. Here if the figure has the null value a null pointer exception will be returned, otherwise the length of the variable will be returned. Let's see what we get from the second line. Let us comment the safe call operator out, and then I'll code. You see? We get an exception here at line number 19. There is another operator known as the Elvis operator in Kotlin which helps us deal with the null pointed exception. It is represented by question mark followed by colon. Now how do we use it? So in the traditional way, we would have found out the length of the figure this way. We would have [INAUDIBLE] if it is not null assign the value of the length to the variable len, otherwise assign it the value of zero. But using the Elvis operator, this can be done in a different way, in a single line of code. Let's see how. So how the Elvis operator works. It first takes a figure follows by the safety operator dot length is null It will return the expression at the right to it, but if it is not null it will return its value. In the Elvis operator the full value is returned if the expression to the left of the Elvis operator has the value null. So there is no chance of getting a null value if we use the Elvis operator. Instead, a different value will be printed. Let us now run the code and see what happens. So here we have LAN is equal to one, since the value of figure dot length is null, the Elvis operator assigns the value to the right of it to the length variable. So this is what null point of exception in Kotlin is all about. I hope you now deal the handling of non pointed exceptions in Kotlin. That's all for this lesson. In the next lesson we will look the Kotlin's string template, how interpolation logs in Kotlin. And then stay tuned and have a good day.

Back to the top