Programming in Kotlin: Fundamentals

Aug 9 2022 · Kotlin 1.6, Android 12, IntelliJ IDEA CE 2022.1.3

Part 3: Functions & Nullability

20. Create & Consume Nullables

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 19. Introduction Next episode: 21. Challenge: Use Nullables

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

So far, all the constants and variables you've used and created had values. However, in programming, that's not always the case. This is why in most programming languages, a concept exists which describes values which, well, don't really have a value. This concept is called null. Null or no value is often used to describe a value which may or may not be there. For example, I usually carry my keys around with me at all times, but I also tend to lose them in my backpack or jacket and I just can't seem to find them anywhere. In that case, they're null to me. In development, it's common to face a situation where you might not know if a value is there or not. Another good example is working with files. If you try to open a file path which don't exist, you'll get a null file. Now, developers are humans and they sometimes make mistakes in code. Unfortunately, ending up with null values. Null is often referred to as the billion-dollar mistake. This is because it's estimated that null values and errors in programs have resulted in billions of dollars of repairs, patches, and software changes. However, in Kotlin, you can avoid losing so much money by using a built-in system called the Nullable Type. Nullable types are types which allow you to use null to represent a constant or a variable. If a value is not marked with a nullable type, it cannot be null. You get a compile time error if you try to assign null to it. This make sure you don't do any funny business with non-nullable values. To create a nullable type and values, there is a pretty simple convention and syntax. All you have to do is to add a question mark at the end of a type to tell the program that this value may not exist and you can assign null to it. Then, whenever you want to use a nullable value, you have to apply specific type of calls and checks, otherwise you get more compile time errors which stops you from breaking your program accidentally. Let's see how to save a couple billion dollars using nullable types in Kotlin. When I was in high school, we had about three students bearing the same name as mine, Emmanuel. For this reason, each Emmanuel had their own nickname so that we could tell who is who. My nickname at that time was stretchy, which was because I was the tallest in the class. But over the years, I've gained new nicknames and I've lost that high school one. And quite frankly, I don't have one right now. You could say that my nickname is currently null because I have none. Let's change this constant to represent that. First, you have to let the compiler know that it's a nullable value by explicitly assigning its a nullable string type like so. I added a question mark notation at the end of the type. You can now assign it the value of null and print it out. Run the project and you'll see null is printed out. Let's see what happens if you try to assign a null value to a non-nullable type. Add another constant to the properties, name it last name and assign null to it. You can see the ID complaint. And if you run the project, you'll get a compile time error that you cannot assign null to a non-nullable value. This is to keep you from accidentally breaking your code and possibly losing billions of dollars. Once you change the type to be a nullable string, it will work. But how can you use the values now, or access their properties like the strings length? Let's try getting the length of the nickname. This just doesn't work. The error might be a bit hard to see, but if you hover over dot operator, you'll see that the compiler is not happy about it, and that it gives you different options to fix the issue. I'll click on the dot operator, then click the red bulb icon. You can see we have a menu with two options. Select add non-null asserted call. Two exclamation marks are added before the dot, and this fuses the value, guaranteeing to the compiler that it is not null. This is one of the thing you should try to avoid when dealing with nullables, the non-null asserted operator. It fuses the value to be a non-null, but if it is null, your program will crash. You use this when you are very sure that the variable or constant would have a value. To show you what I mean, print out this constant. Run the program now and you should see an exception in the run panel. An exception is primarily an event which signals that something went wrong during program execution. You'll learn about exceptions as you progress in your Kotlin development journey. Now, a better way to deal with this case is to use the safe call operator. This was the other option that appeared on the Fix menu when you clicked the red bulb icon few more moments ago. You use a safe call by adding a question mark before the dots like so. The safe call operator will execute and return the right-hand side of the operator if the value is not null. If it is null, it'll return null instead. This means that if you call the length with a safe call like we have here, you essentially get back a nullable hint since the value can be the length which is an hint or null if the nickname is null. Run the program now and it won't crash. It will print out null instead. The safe call operator is really good, even if you have multiple calls change, as you can also change your operators like so. Yeah, you can see a change call to double. If any of the safe call operators fail due to a null value, the entire expression will be null. If not, they will get executed as if there are no null values included and you'll transform the value to what you need. Change the nickname to be stretchy. Then, run the program once more to see that you get a double type value of the nickname's length. Now, sometimes you want to check if a value is not null and then work with a non-null version of a nullable value if the check succeeds. That's a lot of nulls in just one sentence. This is called null check. Let me show you what I mean. Enter the following code. You open an if statement. And then within it, check if the value is different from null. You can then proceed to work with that value within the scope of the if block. Now, remember, last name is a nullable string. But notice how when you hover over the last name inside the body of the if statement, the editor says it has been smart cast to string. This is another cool mechanism Kotlin has where if you check vulnerability and values that won't change like constant and they are not null, the compiler will know it can change the type of the value to be a non-nullable type. This is called casting to a type, or smart casting to a non-nullable type. Then, you can freely use it in the rest of the scope as a non-nullable type. In this case, the scope of the if statement. With this, you don't need to put a question mark before the dot operator when accessing the length of the string because Kotlin has smart casted it. Run the program now. And because you didn't put in the last name, you should see the else block printed out. Using checks and safe call operators are closely tied, as you can use a safe call operator to verify some data using an if and then smart cast it and the rest of the if block like so. This code looks correct, but it seems to have an error in the code. Hoover over it. It says that the if statement required a Boolean, but a nullable Boolean was found. And this is simply because nickname is a nullable string which means that it might have null value. So, there is empty method that returns a nullable Boolean. To solve this, click on the bulb icon and select add equals to true. With this, the expression will always return a Boolean value. Now, within the print statement, the nickname is smart casted to a non-nullable string. There are other ways you can ensure your values are not null without using an if. You can use the Elvis operator. Enter the following code to see it in action. It is called Elvis because when you tilt your head, it looks like Elvis Presley's hair. What it does is pretty simple. It returns the left-hand side if the value is not null and the right-hand side if it is null. This means that for the current nickname, it'll return stretchy. But if I were to change it to null, it'll return my name instead. Run the project, and you can see it returns to stretchy. Now, change a nickname to null and run it again. It returns Emmanuel instead of null this time around.