Nulls in kotlin

Updated 29 November 2019

Save

One of the many things that most of the developer fear about is NullPointerException and how to avoid NullPointerException properly.

Anyway, using Kotlin didn’t make the NullPointerException disappear at once. The thing is that nullable types are no guaranty you won’t have NullPointerExceptions anymore. They’re just a very powerful tool to prevent them.

Well first of all we all need to know what is null and Null Pointer Exception?

How handle nulls in kotlin?

Enough with theory let’s dive into little bit of coding:

Normally a variable cannot hold a null value, because in kotlin you cannot assign a null value directly to a variable otherwise it will show a compile error.

but we can add a ? after the data type of that property which declares that variable as a can also hold the null value.

In the above example, we have to check every time before using the variable.

 

There are other ways we can check for variable is null or not.

Explicitly checking for Null(an older way.)

We mostly use the this condition in various other languages like C, C++, Java, etc.

Using Safe Calls (?.)

Another way of using a nullable property is safe call operator ?. The another way to avoid NPE(NullPointerException) is using the safe call(?.). The (?.) calls the method and checks if the property is not null or returns null if that property is null without throwing a NPE (NullPointerException).

Let

To perform this type of operation in only for non-null values in list or in variable , then you can use the safe call operator with let

Elvis Operator (?:)

This one is similar to safe calls except the fact that it can return a non-null value if the calling property is even null

The Elvis operator will evaluate the left expressions and will return the value if it’s not null otherwise the right side expression will be executed. The right side expression will only be called if the left side   expression is null.

The null check !! Operator

The null check !! operator is used to explicitly to tell the compiler that the property is not null and if it’s null it will throw a null pointer exception NPE (NullPointerException).

 Note: The above example will work fine if ‘student’ is not null otherwise it will throw an NPE(NullPointerException).

The basic difference while using ?. and !! is if are not sure the variable may or may not have a null value then use ?.
But if you are sure that the variable value is not null use !! instead of ?.

Also, ?. can be used to return or throw a different kind of exceptions but !! will only throw an NPE.

. . . . . . . . . . . . . . . .

 

That’s all for now, Thanks for reading, I hope this blog will help you.

 

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home