October 2021
Intermediate to advanced
500 pages
16h 23m
English
NullPointerExceptions like the one you saw above should be avoided at all costs. The Kotlin language and compiler have several techniques that they use to help prevent this kind of crash. First and foremost: Kotlin prevents you from assigning a null value to a variable of a non-nullable type. If you want a value to be nullable, you must explicitly opt in.
To mark the type as nullable, place a question mark after the name of the type, like String?. Fix the compiler error introduced in Listing 7.1 by updating the return type of obtainQuest:
Listing 7.2 Using a nullable type (Main.kt)
... private fun obtainQuest( playerLevel: Int, playerClass: String = "paladin", hasBefriendedBarbarians: Boolean = true, ...
Read now
Unlock full access