Search code examples
androidkotlindata-class

Kotlin data classes and nullable types


I'm new to Kotlin and I don't know why compiler complains about this code:

data class Test(var data : String = "data")

fun test(){
  var test: Test? = Test("")
  var size = test?.data.length
}

Compiler complains with test?.data.length, it says that I should do: test?.data?.length. But data variable is String, not String?, so I don't understand why I have to put the ? when I want to check the length.


Solution

  • The expression test?.data.length is equivalent to (test?.data).length, and the test?.data part is nullable: it is either test.data or null. Therefore it is not null-safe to get its length, but instead you should use the safe call operator again: test?.data?.length.

    The nullability is propagated through the whole calls chain: you have to write these chains as a?.b?.c?.d?.e (which is, again, equivalent to (((a?.b)?.c)?.d)?.e), because, if one of the left parts is null, the rest of the calls cannot be performed as if the value is not-null.