vendredi 3 avril 2020

Non nullable type throwing KotlinNullPointerException

I have the following class in java

public class MyZClass1_ {

    public int x = 100;

}

and that test in kotlin

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class _01_SO_KotlinReflectionAccessorReturnTypeKotlinNull {

    @Test
    fun demonstrate_returnTypeReflectionOfAccessorFunctionInJavaClassThrowsKotlinNull() {
        val zClass1_ = MyZClass1_::class
        for (declaredProperty in zClass1_.declaredMemberProperties) {
            val propertyGetter = declaredProperty.getter
            assertNotNull(propertyGetter)
            try {
                val returnType = propertyGetter.returnType
            } catch (ex: KotlinNullPointerException) {
                ex.printStackTrace()
            }
        }
    }

}
  • propertyGetter is not null (as asserted)
  • propertyGetter.returnType has a non-nullable type KType
  • The problem is that the line val returnType = propertyGetter.returnType throws a KotlinNullPointerException.
  • Why is that? Am i missing something?




Aucun commentaire:

Enregistrer un commentaire