google-developer-training / kotlin-bootcamp

Report issues with the Kotlin Bootcamp for Programmers codelab
7 stars 2 forks source link

Kotlin Bootcamp issue: wrong code on lesson on nullability? #237

Open yooygn opened 3 weeks ago

yooygn commented 3 weeks ago

Describe the problem There is an error in the code below because the type of the fishFoodTreats variable is Int when it should be Int?. fishFoodTreats is defined as a non-null Int but the safe call operator ?. is intended for use with nullable types.

var fishFoodTreats = 6
fishFoodTreats = fishFoodTreats?.dec()

Here's a corrected version:

var fishFoodTreats: Int? = 6
fishFoodTreats = fishFoodTreats?.dec()

In which step of the codelab can this issue be found? Lesson 2, 4: "Task: Learn about nullability"

Steps to reproduce? Copy and paste

var fishFoodTreats = 6
fishFoodTreats = fishFoodTreats?.dec()

as shown in the lesson and run it in the Kotlin REPL.

Versions IntelliJ IDEA 2024.2.4 (Ultimate Edition)

Additional information None

codelab: kotlin-bootcamp