NullPointerException from Boolean

后端 未结 3 1329
独厮守ぢ
独厮守ぢ 2021-01-08 00:26

This is one for the java purists I think. I recently had an issue with a method to perform a custom parsing of String values to a Boolean. A simple enough task, but for some

3条回答
  •  北海茫月
    2021-01-08 01:12

    Interesting but no answer tells you why this happens in the first place.

    This has to do with the ternary expression.

    The compiler interprets null as a null reference to Boolean, applies the autoboxing/unboxing rules for Boolean (on a null) => you get a NullPointerException at runtime.

提交回复
热议问题