Null Safety

Because Kotlin distinguishes between nullable and non-nullable types, the compiler is aware of the possibly dangerous situation of asking a variable defined as a nullable type to do something when the variable might not exist. To shield against these dangers, Kotlin will prevent you from calling functions on a value defined as nullable until you have accepted responsibility for this unsafe situation.

To see what this looks like in practice, reinstate your call to replace on quest:

Listing 7.5  Using a nullable variable (Main.kt)

...
private fun readBountyBoard() {
    val quest: String? = obtainQuest(playerLevel)
    val censoredQuest = quest.replace("Nogartse", "xxxxxxxx") println( """ |$HERO_NAME approaches the bounty board. ...

Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.