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?
- null:It contains no value at all. So when a variable points to null it points to nothing. If it is assigned to a variable that means that variable has no value at all and It contains nothing.
- Pointer: Every variable has a memory and every memory has its own address and location. The pointer is a special variable that contains the address of the specified variable. It provides direct access to that variable’s value and addresses. Like in c we use:
12int number=10;int *pointer=number;//number is a variableNote: “pointer” now has the address of “number”.
- Null pointer: A null pointer is a pointer that does not point to any object or function.
- Null pointer Exception: null pointer is a runtime exception. Null pointer exception is thrown when an application tries to use an object or variable reference that has the null value in it.
In Java, you can directly assign null to any variable or object type. But as it creates so many loopholes and crashes. Kotlin comes up with a great solution to this.
How handle nulls in kotlin?
- An android developer always knows, most of time apps get crashed due to the NullPointerException.
- In kotlin, you cannot directly assign ‘null’ to any variable or object.In kotlin no variable, by default, can be set to null.
- To differentiate between the null variable and normal variable. In kotlin, you have to add ? (question mark) at the end of the variable.
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.
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.
- Explicitly checking for Null(an older way.) .
- Safe call ?.
- Using let
- A null check !!
Explicitly checking for Null(an older way.)
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. } |
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).
1 |
student?.getAllSubject() |
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
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. |
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
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
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.