eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Enum.values() throws NullPointerException if switch statement exists

Open
#1,450 5 comments 0 reactions 1 assignee Claimed by @srikanth-sankaran View on GitHub
bug
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 10h
Merged PRs (30d)
49

Description

Following class will compile but throws an error when calling the "values()" method on the enum class.
With source level 1.8 this works but source level > 1.8 will throw

```
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException: Cannot read the array length because "" is null
at test_bug/test.FooEnum.values(FooEnum.java:1)
at test_bug/test.Foo.$SWITCH_TABLE$test$FooEnum(FooEnum.java:15)
at test_bug/test.Foo.(FooEnum.java:15)
```

```
public enum FooEnum implements Foo {

A, B;

public static void main(String[] args) {
FooEnum.values();
System.out.println("Works");
}
}

interface Foo {

// random field breaks byte code
String RANDOM_STRING = "bar";

// unused switch
default String breaksCompilation(FooEnum enumValue) {
switch (enumValue) {
case A:
return "A";
default :
return "";
}
}
}
```
The error can only be reproduced as long as interface "Foo" contains a switch statement AND a field. If "RANDOM_STRING" or the switch statement is removed the error will disappear.

Tested with ECJ version 3.31

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.