Nullable Variables

Peter Sommerhoff
A free video tutorial from Peter Sommerhoff
Developer & Software Engineer - With 45,000+ Happy Students
4.5 instructor rating • 6 courses • 62,662 students

Lecture description

In contrast to languages like Java, Kotlin offers null safety through explicit nullable types!

Learn how to create nullable variables and why you should avoid this. You will also understand the advantages of having to make variables explicitly nullable to avoid NullPointerExceptions.

Learn more from the full course

Kotlin for Beginners: Learn Programming With Kotlin

Learn Kotlin from scratch! Grasp object-orientation and idiomatic Kotlin to realize coding projects and Android apps!

06:31:16 of on-demand video • Updated November 2020

  • Create professional applications using Kotlin, the new Java-based programming language developed by Jetbrains
  • Understand the concepts of the Kotlin language and how it integrates neatly with Java
  • Understand the basics of object-oriented software development, the most important development paradigm
  • Understand the principles behind other object-oriented languages like Java, C++, PHP, C#, Scala, or Swift
  • Use Intellij, the popular Java (and Kotlin) IDE, to write code effectively and professionally
  • Read code and write your Kotlin code as well
English Hey guys, in this video, we're going to have to talk about a programming language concept that is kind of dangerous. That is the null reference. Null is basically a special value that you can assign to a variable, and the null basically represents a missing reference, or an empty reference which is basically an empty variable. So, when you're programming language like Java, you could actually write something like: String string = null and that would be fine. Now, if you have a null reference like this, what can happen is you may want to do something with this variable called string. For example, you might want to know the length of the string which would be string.length in Java but what's going to happen is this is going to throw a null pointer exception because string does not actually contain a reference to a string variable but just a special null value. Basically, there's nothing there. I want to have the length of this nonexistent string, and of course, that's not possible. Java, in this case, will throw a so called NullPointerException, and that's the most frequently occurring kind of exception in Java so it's a pretty big dea to avoid these. Now, let me just quickly remove this because it's not Kotlin code. Actually, the inventor of this null reference is called Tony Hoare and he's a really brilliant guy. He actually apologized for inventing this null reference. That was in 2009 and it's a really popular quote by now. He said, "I call it my billion dollar mistake." We're talking about 1965 here, he was working on a type system for an object-oriented language, just like Java is today. Basically, out of laziness, he just included this null reference because it was easy to implement. He realized that this has probably cost companies at least a billion dollars by now so we should try to avoid null references in our code as far as possible. Kotlin does a pretty good job at this. Kotlin aims to completely remove null pointer exceptions from our code and this is called null safety. How is Kotlin different from what we've seen before? Now, first of all, if you create a variable in Kotlin, just like we did before, we can actually not assign the null value to it. If you try to do that, Kotlin is going to throw an error, and it's telling us that we cannot assign null to this type. This is the case with all the types we've seen so far so the same holds if I create an Int and try to assign null to it. It's all the same because none of these basic data types can hold null reference. As long as we are working with data types like these that we've seen so far, here's absolutely no way we can create a NullPointerException, unless we're using code from the Java library that can throw NullPointerExceptions. Our Kotlin code itself will never throw a NullPointerException if we stick to these basic data types. We also do need null references in Kotlin. For example, just to be able to interact with Java libraries which may return null values. The question is how do we create a so called nullable variable in Kotlin? The answer is it looks pretty similar to what we've done so far. There's just one little difference and that is after the name of the data type, we're going to have to put a question mark which is a nice notation for it because, well, it could be a string but you're not so sure. It may also be null so String?. Now, what we can do is we can assign null to this. This is going to be fine. If we try to print str null, it's going to give us null. Now, we can try to call a function on the string variable. If we try to do that, Kotlin is going to tell us that we cannot do this. It's not going to throw a NullPointerException. If we try to compile code like this it just won't even compile in the first place. There can be no NullPointerException at one time. Now, of course, we can still assign real strings to this variable such as, "Kotlin is fun." Now, of course, we can print this again. Now, if we try to call the length method, it's still going to tell us that, "Nope, this is of type String?," so it's a nullable type. Even if it does contain a string at this point, it's still not going to allow us to call a method like length on it because the data type is still a nullable and we cannot do that. Now, there are ways around this because we still need to be able to do something with this variable. The first thing we can do is we can do a so called safe call We can do that by specifying a question mark behind the variable name and then typing in the function we want to call like length. What this is going to do is if string is actually null, this is going to return null but if string is not null it's going to call the function on it. Let's try to run this. This is going to give us 14. That's the length of this string. Just to demonstrate it, I'm going to assign null to it again. Now, if you do the same call on it using the question mark notation, you're going to get null. The return type of this call right here is actually a nullable Int. So if I want to store this in a variable, I would have to make it a nullable Int type, and then I can store the result of this call. I have to scroll down to see it. Now, if I print I it's going to be null again, just like before. The second thing we can do is we can actually assure Kotlin that we know that it is not going to be null, so that Kotlin is going to call the method on it without caring about whether it is null or not. This is the only way we can provoke NullPointerExceptions. Because if we tell Kotlin we are sure the variable cannot be null at this point of execution, but it is null then Kotlin is going to trust us and throw a NullPointerException because we've been wrong. How do we do that? We can type the name of a variable and put two exclamation marks behind it. Then we're going to put it in the name of the function we want to call. Remember, my str variable still contains null. I should not do this because by using the two exclamation marks, I'm telling Kotlin I'm sure that my variable is not going to be null. If I run this null, this is going to throw a KotlinNullPointerException That's just my fault and not Kotlin's fault because, well, I assured Kotlin I know str is not going to be null. All in all, Kotlin does a really good job at preventing NullPointerExceptions, which are the most frequently occurring type of exception so that's a really big deal. Try to avoid nullables wherever you can because then you can be sure it's not going to throw a NullPointerException anywhere in your code. If you try to call a function on a variable, this is actually going to work and not crash your program. This is the really cool thing about Kotlin because other widely used languages, like Java for example, don't have this kind of concept. That's why there are many NullPointerExceptions there. I think it's really annoying and hard to avoid. This is the first really cool thing we learned about Kotlin, and that is null safety. All right, I really hope you're enjoying this course, I'll see you again in the next lecture.