In my android project I have overriden onCheckedChanged() like so:
var numberOfPlayers: Int = 0
override fun onCheckedChanged(group: RadioGroup?, checked: Int) {
val chosen = activity?.findViewById<RadioButton>(checked)?.text
numberOfPlayers = chosen.toString().toInt()
}
And I'm confused why numberOfPlayers isn't underlined red as chosen may be null - therefore I'm calling toString() on a possible null value. Why won't this cause a NullPointerException?
.toString() has a safety, meaning if it receives a null value it will return "null" string.
As stated in the official documentation:
fun Any?.toString(): StringReturns a string representation of the object. Can be called with a null receiver, in which case it returns the string "null"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With