Question - How to ensure null safety in Kotlin?
Answer -
One of the major advantages of using Kotlin is null safety. In Java, if you access some null variable then you will get a NullPointerException. So, the following code in Kotlin will produce a compile-time error:
var name: String = "MindOrks"
name = null //error
So, to assign null values to a variable, you need to declare the name variable as a nullable string and then during the access of this variable, you need to use a safe call operator i.e. ?.
var name: String? = "MindOrks"
print(name?.length) // ok
name = null // ok