Native Shopify Mobile App with 20 new features
Kickstart your hyperlocal marketplace in Corona pandemic with a starter guide
Android App Development
iOS App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 18.104.22.168 release ready!
Owner and Founder. Opencart
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.
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.
We mostly use the this condition in various other languages like C, C++, Java, etc.
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).
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
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 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.
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.