Sobes.tech
Junior — Middle

What happens if you access a Nullable variable that contains null and perform the operator ?.?

sobes.tech AI

Answer from AI

In Kotlin, the safe call operator ?. is used to access an object that may be null without the risk of getting a NullPointerException.

If a variable of a nullable type contains the value null, then using ?. will simply return null and will not perform further operations.

For example:

val nullableString: String? = null
val length = nullableString?.length  // length will be null, and no error will be thrown

Thus, the ?. operator prevents attempts to access members of an object if it is null, ensuring safe execution of the code.