elye
08/12/2022, 8:36 AMenum class School(val zone: String, val captain: Student) {
Metricon("New York", Student.David),
Carlisle("London", Student.Paul)
}
enum class Student(val lastName: String, val school: School) {
David("Samuel", School.Metricon),
Solomon("Handsome", School.Metricon),
Saul("Black", School.Metricon),
Paul("Lewis", School.Carlisle),
Joseph("Hardy", School.Carlisle),
John("Baptise", School.Carlisle),
}
When I print it out
val student = Student.David
val school = School.Carlisle
println(student.school)
println(school.captain)
println(student.lastName)
println(school.zone)
It resulted in
Metricon
null
Samuel
London
Notice the null
there. The school.captain
is missing and became null.
How can I solve this problem?elye
08/12/2022, 8:37 AMRoukanken
08/12/2022, 8:48 AMclass A() {
val x: String = initX()
val y: String = "value"
fun initX(): String = y
}
A().x // is null
eg, at the time x
was "calculated", the y
was not yet initialized, and was therefore null
as all objects within JVM before initialization, and therefore is null even though types don't allow that
In your case: you first accessed Student.David
, which started initializing Student
enum, but to init the first David
, it needed to access School.Metricon
so the School
enum started initializing - but at that point, every other Student
than David
is still nullMichael de Kaste
08/12/2022, 9:08 AMenum class School(val zone: String) {
Metricon("New York") {
override val captain: Student
get() = Student.David
},
Carlisle("London") {
override val captain: Student
get() = Student.Paul
};
abstract val captain: Student
}
Joffrey
08/12/2022, 10:28 AM