# NPE for reflection field in 1.5

**URL:** <https://discuss.kotlinlang.org/t/npe-for-reflection-field-in-1-5/23110>\
**Category:** Language Design\
**Created:** [October 5, 2021, 7:56am UTC](https://discuss.kotlinlang.org/t/npe-for-reflection-field-in-1-5/23110 "2021-10-05T07:56:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![EaseTheWorld](https://avatars.discourse-cdn.com/v4/letter/e/3e96dc/32.png) [@EaseTheWorld](https://discuss.kotlinlang.org/u/EaseTheWorld)\
**Post date:** [October 5, 2021, 7:56am UTC](https://discuss.kotlinlang.org/t/npe-for-reflection-field-in-1-5/23110/1 "2021-10-05T07:56:41Z")

</div>

Hello. I’m developing android app and I’ve met weird NPE since I migrate from 1.4.30 → 1.5.21.  
It seems like a?.b is treated as non-null in 1.5.21.  
(java-decompiled code is same for 1.4.30, 1.5.21)

What’s happening here?

```auto
class A(
    val b: B
)
class B(
    val c: String
)
fun createByReflection(): A? = A(B("aaa")).apply {
    val field = javaClass.declaredFields.find { it.name == "b" }!!
    field.isAccessible = true
    field.set(this, null)
}
fun main() {
    val a = createByReflection()
    println(a?.b) // this outputs null of course.
    println(a?.b?.c) // In 1.5.21, this throws java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String B.getC()' on a null object reference
}

```

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [October 5, 2021, 8:41am UTC](https://discuss.kotlinlang.org/t/npe-for-reflection-field-in-1-5/23110/2 "2021-10-05T08:41:49Z")

</div>

Seems reasonable, `b` is non-null in `A`. Yet you’re hacking around with reflection to change that.  
The compiler is very reasonable here in assuming `b` is not null.

---

<div class="post-metadata">

**Author:** ![EaseTheWorld](https://avatars.discourse-cdn.com/v4/letter/e/3e96dc/32.png) [@EaseTheWorld](https://discuss.kotlinlang.org/u/EaseTheWorld)\
**Post date:** [October 5, 2021, 9:51am UTC](https://discuss.kotlinlang.org/t/npe-for-reflection-field-in-1-5/23110/3 "2021-10-05T09:51:32Z")

</div>

Yes it’s understandable. but  
was there release note about this change?

2021년 10월 5일 (화) 오후 5:52, Alberto Arri via Kotlin Discussions \<[kotlinlang@discoursemail.com](mailto:kotlinlang@discoursemail.com)\>님이 작성:

---

<div class="post-metadata">

**Author:** ![seekdasky](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/seekdasky/32/4673_2.png) [@seekdasky](https://discuss.kotlinlang.org/u/seekdasky)\
**Post date:** [October 5, 2021, 11:07am UTC](https://discuss.kotlinlang.org/t/npe-for-reflection-field-in-1-5/23110/4 "2021-10-05T11:07:59Z")

</div>

If your code rely on an undefined behaviour (which your code very much is) , you are on your own. the Kotlin team probably don’t have test for that case and won’t add one, thus the lack of changelog for that.

---

<div class="post-metadata">

**Author:** ![EaseTheWorld](https://avatars.discourse-cdn.com/v4/letter/e/3e96dc/32.png) [@EaseTheWorld](https://discuss.kotlinlang.org/u/EaseTheWorld)\
**Post date:** [October 6, 2021, 5:49am UTC](https://discuss.kotlinlang.org/t/npe-for-reflection-field-in-1-5/23110/5 "2021-10-06T05:49:28Z")

</div>

If it is ‘undefined behavior’ then I agree that it doesn’t have to be in release note.  
but I still wish there was a note or something.  
(I used reflection directly in my example, but it was Gson in the production code.)

---

<div class="post-metadata">

**Author:** ![seekdasky](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/seekdasky/32/4673_2.png) [@seekdasky](https://discuss.kotlinlang.org/u/seekdasky)\
**Post date:** [October 6, 2021, 8:11am UTC](https://discuss.kotlinlang.org/t/npe-for-reflection-field-in-1-5/23110/6 "2021-10-06T08:11:59Z")

</div>

Ah, I see, that is unfortunate. If it is possible I would advise you to migrate to `kotlinx.serialization`, it’s reflection-free, written in kotlin, maintained by JetBrains and works a treat 🙂

---

<div class="post-metadata">

**Author:** ![EaseTheWorld](https://avatars.discourse-cdn.com/v4/letter/e/3e96dc/32.png) [@EaseTheWorld](https://discuss.kotlinlang.org/u/EaseTheWorld)\
**Post date:** [October 7, 2021, 7:36am UTC](https://discuss.kotlinlang.org/t/npe-for-reflection-field-in-1-5/23110/7 "2021-10-07T07:36:50Z")

</div>

Thank you. I’ll try kotlinx.serialization.

---

<div class="post-metadata">

**Author:** ![Alexey.Belkov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexey.belkov/32/5215_2.png) [@Alexey.Belkov](https://discuss.kotlinlang.org/u/Alexey.Belkov)\
**Post date:** [October 7, 2021, 1:30pm UTC](https://discuss.kotlinlang.org/t/npe-for-reflection-field-in-1-5/23110/8 "2021-10-07T13:30:40Z")

</div>

Thank you, reported [https://youtrack.jetbrains.com/issue/KT-49136](https://youtrack.jetbrains.com/issue/KT-49136)

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [October 7, 2021, 1:41pm UTC](https://discuss.kotlinlang.org/t/npe-for-reflection-field-in-1-5/23110/9 "2021-10-07T13:41:41Z")

</div>

Ohh, I’m actually surprised this went to YouTrack. Why is it considered a bug? I guess the generated bytecode should not check not-null variables for nulls. Or maybe we assume that `?.` operator should enforce such check, even if used on a not-null?

---

<div class="post-metadata">

**Author:** ![EaseTheWorld](https://avatars.discourse-cdn.com/v4/letter/e/3e96dc/32.png) [@EaseTheWorld](https://discuss.kotlinlang.org/u/EaseTheWorld)\
**Post date:** [October 7, 2021, 3:14pm UTC](https://discuss.kotlinlang.org/t/npe-for-reflection-field-in-1-5/23110/10 "2021-10-07T15:14:19Z")

</div>

Interesting. So this is not intentional change?  
Moreover, in KT-49136, it is not reproduced in 1.6…  
I’ll see how it goes.
