I got this error:
Error:(36, 13) Kotlin: Null can not be a value of a non-null type String
running this code:
var drink = readLine()!!.capitalize()
drink = null
println(drink)
Why does the compiler not use String? type automatically?
Furthermore this listing seems to be a wrong example, again.
Because the line
beverage = null
is beyond the .capitalize function call, the listing only prints null instead of throwing a null pointer exception. If the beverage = null would be placed before
var beverage = readLine()!!.capitalize()
the capitalize function would use the input from readLine() and then the null assigned to beverage before would be overwritten with this capitalized input.
This is throwing an null pointer exception for me:
var drink:String? = null
drink!!.capitalize()
println(drink)
Best Regards