Integrated: 8272776: NullPointerException not reported

Jan Lahoda jlahoda at
Fri Sep 3 09:35:28 UTC 2021

On Tue, 31 Aug 2021 07:25:12 GMT, Jan Lahoda <jlahoda at> wrote:

> Code like:
> public void foo(Object o) {
>         switch (o) {
>           default: break;
>           case String s :System.out.println(s); break;
>         } 
> }
> public static void main(String[] args) {
>         (new X()).foo(null); // NPE expected, but prints null
> }
> Should lead to a NPE when executed, because `default` does not cover `null` and there is no total pattern in the switch, but the NPE is not thrown. The reason is that when generating the code, a `hasTotalPattern` flag is used, which means "has a total pattern or default". The code needs to check whether there is a real total pattern (which covers `null`) or not. That is done by looking for the `default` case label, but the code only checks the last case for `default`. It needs to check all cases to differentiate between a switch with a real total pattern and a switch with a `default`.

This pull request has now been integrated.

Changeset: 93eec9a1
Author:    Jan Lahoda <jlahoda at>
Stats:     18 lines in 2 files changed: 14 ins; 1 del; 3 mod

8272776: NullPointerException not reported

Reviewed-by: vromero



More information about the compiler-dev mailing list