The Kotlin type system helps us to avoid NullPointerException errors in our code. In this video, learn how to use the Let function, safe-call operator, and non-null assertions to safely handle nullable values.
- [Instructor] Now let's take a look at nullability in Kotlin. This is what helps us to avoid null pointer exception errors in our code. So we're here inside of the nullability file, in the test folder. Now as you can see, we have several test methods that we're going to fill in, which will help us to better understand how the Kotlin language protects us from nulls in our code. So the first thing we're going to do is create a function called double string that would duplicate a string's contents here on line five. So just as usual, we start with the fun keyword, it's going to be called doubleString, we're going to pass in a string for the parameter, and we're going to return another string, which is the same one doubled.
So this is just going to be the string that we receive, and then we'll use the repeat function from the Kotlin standard library and we'll supply the number two because we want it twice. Now notice that when we declare our string parameter, we don't have a question mark after the type. This means that whatever value we pass into this function cannot be null. And that's going to affect how we interact with our method. So let's try it out right here, inside of the canNotBeNull function.
So we're going to create a variable, and we'll name it flavor. It's going to be of type String, and we'll set it equal to mint. Now, let's call our double string function. So we're just going to print out the results of calling double string with our flavor variable. Very simple. Everything compiles and we have no issues. But let's say we try to set flavor to null. So right here on line 14, I'm going to take my flavor variable, and set it equal to null.
Notice that we receive an error in our IDE. Becuase we cannot set a null value to the non-null type string. But what if this is really what we want it to do? Let's highlight the same code, and we're going to paste it into line 20, in the canBeNull method. And we're going to leave everything the same except for we're going to add a question mark behind the string type definition. This is how we indicate that we want to open up our variable so that it can point to null as well.
But notice that this causes another error for us down here on line 24. And that's because our double string method will only accept strings that cannot be null. So let's look at some options for dealing with this, as this is a common problem. Especially when you're working with projects that contain both Kotlin and Java. So for now, let's comment out both lines of code that have the errors. And we'll look at our first option in the ifNotNullCheck.
So the first way to handle it is just to do a standard ifNotNullCheck, similar to what you've done in Java for years. So here, on line 29, we're going to paste that same code again, except for this time, we're going to wrap our call to the doubleString method. So let's delete setting it to null, and we're going to do a check on flavor. So if flavor does not equal null, only then will we make our call to our doubleString function.
This works because the compiler remembers that we've compared our variable with null and treats the value as being such inside of that scope where the check has been performed. So this is often the ideal solution that you want to go with. But let's look at another option. We're going to take the let function, combined with this safe call operator. So we'll come right down here, inside of the letCheck method, and paste our code again. Except for this time, on line 38, let's set it to null directly.
And then we won't need that extra line there, on 39. And to use the let function, we just call our variable flavor, with the safe call operator which is the question mark and the dot, and then we can call let directly. Let takes in a lambda, so we'll use our curly braces. And then we'll just close it up. Now, notice that we still have an error here on line 38. That's because we want to make sure our flavor is set to a nullable string.
So just like that, everything works. If we run this code, nothing gets executed. And why is that? That's because at this point, once the let function is called, it checks to see, well, is my variable null or not? If it is null, then it does nothing. If it's not null, then it will go and execute what's inside the lambda. Now let's copy the same code, and move into our next option. And that's the Elvis operator.
That's the name given to the combination of the question mark with the colon. So let's come inside of the elvisCheck method, paste our code, and this time, we're going to delete line 49, because we're not working with let. And inside of the doubleString method directly, we'll add the Elvis operator. Now this is where we can say, hey, if our variable is not null, then use it.
But if it is null, I want you to use this placeholder. And so for our placeholder, we're going to provide another string, which we call something else. So let's run this code. And as you can see, it prints out something elsesomething else. That's because of the Elvis operator allowing us to have a fallback option. And finally, we're going to look at the not null assertion.
So we can copy our code here from the Elvis check, and let's move along right inside the notNullAssertion method. So instead of using this code for the Elvis operator, we're going to replace all of it with double exclamation marks. And notice once again, we didn't receive an error from our call site. That's because the double exclamation mark will convert any value to a non-null type. Now, this is the simplest way to work with nulls, but it's often discouraged because it takes away the ability of the compiler to verify your assertion.
So use it sparingly, or not at all. Now although we use the string class in this video, the nullability rules and patterns apply to any type. Even ones that you create yourself.
- What Kotlin has to offer
- Working with lambdas
- Common Android extension functions
- Kotlin Android Extensions overview
- Making use of the Anko library
- Working with coroutines
- Nullability and collections