Kotlin

Kotlin Caveats

Calling a toString() on a nullable type

A thing to look out for when using the toString method in Kotlin is the handling of null in combination with the String?.

For example you want to get text from an EditText in Android.

You would have a piece of code like:

// Incorrect:
val text = view.textField?.text.toString() ?: ""

You would expect that if the field did not exists the value would be empty string but in this case it is "null".

// Correct:
val text = view.textField?.text?.toString() ?: ""

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow