“unexpected type” error when comparing enum values
-
19-01-2021 - |
Question
Most people I've seen with this problem were using =
where they needed ==
. What's causing my problem here?
com\callmeyer\jopp\FieldCoordinator.java:303: unexpected type required: class, package found : variable if (event.getType() == event.Type.INSERT) { ^
The enum definition and accessor:
public class DataLayoutEvent {
public static enum Type { INSERT, DELETE, RENAME, MOVE, RESIZE }
private Type type = null;
public Type getType() {
return type;
}
...
}
and the method where the error occurs:
public void layoutChanged(DataLayoutEvent event) {
if (event.getType() == event.Type.INSERT) {
fieldAdded(event.getField(), event.getToIndex());
}
...
Solution
Use static access instead of instance access:
if (event.getType() == DataLayoutEvent.Type.INSERT) {
You can (but shouldn't) use instance access for static members (methods and fields), but not for inner types.
OTHER TIPS
It should just be:
// From within DataLayoutEvent
if (event.getType() == Type.INSERT) {
or
// From other classes
if (event.getType() == DataLayoutEvent.Type.INSERT) {
The Type
part is the name of a type - it can't be qualified by a variable value (event
). You could import DataLayoutEvent.Type
if you wanted to use the first form from elsewhere, by the way.
I think you need to refer to Type
differently:
if (event.getType() == DataLayoutEvent.Type.INSERT) { ... }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow