英文:
Non nullable type throwing KotlinNullPointerException
问题
我在Java中有以下的类:
public class MyZClass1_ {
public int x = 100;
}
然后在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` 不为 null(如断言所示)
- `propertyGetter.returnType` 有一个非空类型 `KType`
- 问题是这一行 `val returnType = propertyGetter.returnType` 抛出了 `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 typeKType
- The problem is that the line
val returnType = propertyGetter.returnType
throws aKotlinNullPointerException
. - Why is that? Am i missing something?
专注分享java语言的经验与见解,让所有开发者获益!
评论