I have a codebase of 10k lines and this bug was very hard to find, after hours of debugging
I finally created a minimal example where this bug is reproducible… Please, I’m in need to resolve this
as soon as possible since I need to go to the production, but this bug is a roadblocking deploy.
If I switch UUID
with Int
the NullPointerException
will not occur.
This code…
@JvmInline
value class Id<T>(val value: UUID = UUID.randomUUID()) {
override fun toString(): String = this.value.toString()
}
data class Parent(val id: Id<Parent>?)
fun main() {
val parent = Parent(id = null)
val kProperty1 = Parent::class.memberProperties.first()
assert(kProperty1 == Parent::id)
println("Key '${Parent::id}' has value: '${Parent::id.get(parent)}'")
println("Key '$kProperty1' has value: ...")
println(kProperty1.get(parent))
}
Creates this output…
Key 'val org.example.Parent.id: org.example.Id<org.example.Parent>?' has value: 'null'
Key 'val org.example.Parent.id: org.example.Id<org.example.Parent>?' has value: ...
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.UUID.toString()" because "arg0" is null
at org.example.Id.toString-impl(Main.kt:8)
at org.example.Id.toString(Main.kt:8)
at java.base/java.lang.String.valueOf(String.java:4993)
at java.base/java.io.PrintStream.println(PrintStream.java:1186)
at org.example.MainKt.main(Main.kt:20)
at org.example.MainKt.main(Main.kt)
Can this be resolved in other ways that I’m not aware off? The API of the program must stay the same since this is a deploy requirements…
It seems that memberProperties
has bad implementation… getting property over memberProperties
creates inline object with null value inside and not the null value for the object itself…