Back to questions
JuniorTheoryCommon
What happens when you build and run this program?
fun main() {
val text: String? = null
println("start")
println(text!!.length)
println("end")
}Prints start, then a NullPointerException
- Expecting
start,nullandend: mixing up!!with?..?.givesnulland goes on;!!throwsNullPointerException, soendis never printed. - Expecting a compile error:
!!is what lets a call on aString?compile, so the program builds, printsstartand fails on the!!line.
- How would you print
0instead of failing whentextisnull? - Why is
!!called a last resort?