please help me understand how to properly initialize "pets"
class Person:
class Person(
val height: Int,
val weight: Int,
val name: String,
) {
lateinit var pets: HashSet<Animal>
fun buyPet():Unit{
this.pets.add(Animal((0..100).random(), (0..100).random(), getRandomString((3..12).random())))
}
private fun getRandomString(length: Int) : String {
val allowedChars = ('A'..'Z') + ('a'..'z')
return (1..length)
.map { allowedChars.random() }
.joinToString("")
}
}
class Animal:
data class Animal(
val energy:Int,
val weight:Int,
val name:String) {
}
main:
fun main() {
val person1=Person(187, 85, "Denis")
person1.buyPet()
println(person1.pets)
}
I am getting this error
Exception in thread "main" kotlin.UninitializedPropertyAccessException: lateinit property pets has not been initialized at classes_06.Person.buyPet(Person.kt:35)