String concatenation

var t:String?=null

fun main() {

t="bhoomika"

println("${t.length}")

}
why this code give error

In first line you have declared “t” that it can be null. So whenever you access it you need to use safe call operator which is “?”. So your code should look like this

println("${t?.length?:""}")

So we first check if t is null, if it is null then “”(empty) is printed else t’s length is printed.

You can read about it here
https://kotlinlang.org/docs/reference/null-safety.html

1 Like