Java NPE in ternary operator with autoboxing?

前端 未结 5 1221
离开以前
离开以前 2020-12-09 03:29

I ran across a very weird NPE this morning, and reduced it to a simple example. Is this a JVM bug or correct behavior?

public class Test1 {
    class Item {
         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-09 03:53

    item may not be null, but when you call getId(), that is returning null. When you try to auto-unbox null, you get an NPE.

提交回复
热议问题