Updated 29 November 2019
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.
1 2 |
int number=10; int *pointer=number;//number is a variable |
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.
1 2 |
var student : Student = Student() student = null //compilation 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.
1 2 |
var student : Student? = Student() Student = null //It will work fine |
In the above example, we have to check every time before using the variable.
1 2 3 |
Student.getClass() //In this case the compiler is sure that the variable will not be null Student.getAllSubject() // In this case the compiler will show an error at compile time as the value can hold a null value. |
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.
1 2 3 4 5 |
if (Student!=null) { Student.getAllSubject() } else { // TODO: What to do when nullVar is null. } |
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).
1 |
student?.getAllSubject() |
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
1 2 3 |
var name: String?= String() name=null print(name?.let{"no name found"})// the let will be executed when the name variable is null. |
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
1 |
val result = student?.getAllSubject()?: "No Subject Found" |
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).
1 |
student!!.getAllSubject() |
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.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.