Nulls in kotlin. How kotlin handles the developer's pain “The NPE”
Tags: kotlin, todayilearned, beginners, codenewbie
Published: 2019-07-12 19:27:46

If you ask any android or java developer about the scariest exception, he/she will say only one name and that is NPE or Null Pointer Exception. More than 70% of apps crash in its lifetime due to null pointer exception. Null pointer was one of the most painful exceptions for android and java developers.

That’s why when JetBrains team designs the kotlin they put a fair focus on handling null and they came up with a magical solution which is really very effective to provide safety against nulls. Let’s see how beautifully kotlin handles nulls.

##What is null and null pointer exception?
---
* ***null —*** it contains no value at all. If it is assigned to a variable that means that variable has no value at all. It contains nothing. So when a variable points to null it points to 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.

* ***Null pointer —*** In computer programming, 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 kotlin handles null?
---
* As every android developer knows, more than 70% of android apps got crashed once due to null pointer exception.

* Kotlin introduces a new and secure way to deal with nulls.

* In kotlin, you cannot directly assign ‘null’ to any variable or object.

* ***In kotlin no variable, by default, can be set to null.***

* For differentiating null variables from other normal variables. In kotlin, you have to add ***? (question mark)*** at the end of the variable. *Here type inference won’t work, the data type must be declared explicitly. Otherwise, it will create problems later.*

##Compile time check
---

* Remember once you declared a variable using a question mark. It will become a special kind of variable that only can be accessed by compile-time check.

* Once the variable is declared as ‘null variable’, every time you call it you have to do check whether the variable contains null or not.

* There are two ways to perform the check:
1. Through if else condition
2. Through safe call operator (?.)

####Check through if else condition
```
val b = "Kotlin"
if (b != null && b.length > 0) {
    print("String of length ${b.length}")
} else {
    print("Empty string")
}
```
* It won’t compile if you don’t check first if it’s null.

####Check through safe call operator

* Here checking through safe call operator is more concise and easy to use.
```
val a = "Kotlin"
val b: String? = null
println(b?.length)
println(a?.length)
```

* This returns `b.length` if `b` is not null, otherwise it returns `null`.

* So with this solution at least your app will not crash implicitly via **NullPointerException** anymore.

##!! (Assertion operator)
---
* But consider the case in which you are 100% sure that the nullable variable does not contain the null.

* So in that case to avoid the check you can use assertion operator. Which will suppress the check.

* But in that case compiler is depending on you and if that nullable variable has the null in it. So, in that case, your app will crash and compiler throw NullPointerException.
```
val a: Int? = null
val b= a!!.toDouble()
```

* But this operator is very dangerous and it can put you in trouble of hours of debugging pain and lets your app crashes as well.

* ***So avoid it as much as possible.***

* So in this way kotlin handles ***‘The Billion Dollar Mistake:- The Nulls’***

That’s it for today guys. Thanks for reading. Stay tuned for more content.

Till then Keep Coding, Keep Loving.

Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

- [My Site] (https://www.jaytillu.in)
- [My Blogs] (https://blogs.jaytillu.in)
- [Twitter](https://twitter.com/jay_tillu)
- [Facebook](https://www.facebook.com/jaytillu.1314/)
- [Instagram](https://www.facebook.com/jaytillu.1314/)
- [Medium](https://medium.com/jay-tillu)
- [LinkedIn](https://www.linkedin.com/in/jaytillu/)
- [Github](https://www.github.com/jay-tillu)
- [Stack overflow](https://stackoverflow.com/users/8509590/jay-tillu)
or just mail me at jd.tillu@gmail.com