Welcome back.
And this year, we are going to look at the Alvis operator and not NUL assertion operator.
And in order to do so, I first of all start by commenting our name here because we used it and we saw
how it works.
Right.
But now what I want to do, I want to assign our nullable name to a non nullable variable.
So this value here should be a non nullable.
Right.
But I have another variable here called nullable name and I need to assign it to this variable.
So sometimes you have this situation and you can't get around it.
So what you can do is you can use the.
Elvis operator in order to either assigned a value that is within that nullable name variable, so let's
say the variable has a name in there and otherwise it's just going to have this default value.
So question mark Colon, in order to say, OK, if Nullable is empty, then please use my default value
that I have here otherwise used to value that is in the nullable name.
So now if we print that name, we will see that it should say in this case, Dennis, because the nullable
name is in fact, not now.
So there we are.
Name is Dennis.
But now let's say we make nullable name now and we run this again.
Then we see name is guest, OK, so if the value is now, then it's going to take the default value.
So this one, this question mark colon here that we're using is the Elvis operator.
All right, the next thing that I want to use is to not know assertion operator, which is the double
exclamation mark operator that converts and nullable type to a non null type and throws a no pointer
exception if the nullable type holds a null value, this is risky and you should only use it if you
are 100 percent certain that there will be a value in the variable.
So let's use it here.
I'm going to say nullable name double exclamation mark thought to lower case.
OK, so there is this.
Method called to lower case, and it's going to put everything to a lower case value, so instead of
guests, it's going to be a guest, for example.
All right.
So this here in our case will create the null pointer exception because we are really forcing it.
And it's our own mistake because we didn't make sure that nullable name is, in fact, not now.
So if you can see here, I'm getting a kotlin now, pointer exception in line 17.
So in this line here.
And this has to do with.
Nullable name be know, so you can see it is null at this line, so that's why we get this error.
So now if it's not now, because it was a Saint Denis here and we never change it to know.
So if it's not null, it's going to print Dennis and lowercase.
And your name is Dennis.
So now if we print that.
My name and we get rid of this print statement that we have before now to just print Dennis with a lowercase.
So there we are, Dennis.
All right, so now you have seen how no pointer exception works and how you can force one, you also
have seen how multiples work in Scotland.
And there's one last thing that I want to show you.
But it will not make sense.
You know, it will make more sense once we get into objects and variables.
But I just want to very quickly show you that you can use the safe call operator in a chain.
So here we are using a chain safe call, so to speak.
So let's say we have a class called user and we create an object of it.
There is a wife.
So the user can have a wife, which is also of a class or of a type.
And then this wife has an H.
So what we can do is we can use a chain safe call here, for example, checking if the user is empty
and then also checking if the user's wife is empty.
So if it's now or not.
OK.
So if that's the case, then assign a value of zero.
So as I said, this only makes sense once you understand the concept of object oriented programming.
But if you come from another programming language, that will make now sense already.
But otherwise, I think it makes sense to go into object oriented programming, which we will do in
the next video.
So the next thing that we're going to look at is actually classes and objects.
And this is a little much for now.
But I just wanted to show you this in order to complete this section or this lecture for nullable and
codling.
All right.
So I'd say let's go to the next video where we're going to check out object oriented programming and
how to create our own classes.