Java枚举异常

huangapple go评论78阅读模式
英文:

java enum exception

问题

我有枚举类

package org.testTask.DTO;

public enum Publisher {
    MOSCOW("МОСКВА"),
    ST_PETERSBURG("ПИТЕР"),
    O_REILLY("O’REILLY");

    private String name;

    private long id;

    Publisher(final String name) {
        this.name = name;
        this.id = ordinal();
    }

    public String getName() {
        return name;
    }

    public long getId() {
        return id;
    }
}

SQL 查询

protected Publisher getObject(ResultSet resultSet) throws SQLException {
    return Publisher.valueOf(resultSet.getString("NAME"));
}

和数据库中的变量值为"МОСКВА","ПИТЕР","O’REILLY"。但是它给我一个错误:java.lang.IllegalArgumentException: No enum constant org.testTask.DTO.Publisher.ПИТЕР,我无法理解问题在哪里。

英文:

I have enum class

    package org.testTask.DTO;
    
    public enum Publisher {
        MOSCOW("МОСКВА"),
        ST_PETERSBURG("ПИТЕР"),
        O_REILLY("O’REILLY");
    
        private String name;
    
        private long id;
    
        Publisher(final String name) {
            this.name = name;
            this.id = ordinal();
        }
    
        public String getName() {
            return name;
        }
    
        public long getId() {
            return id;
        }
    }

SQL request

protected Publisher getObject(ResultSet resultSet) throws SQLException {
    return Publisher.valueOf(resultSet.getString("NAME"));
}

and variables in the database with values "МОСКВА", "ПИТЕР", "O’REILLY". But it gives me an error java.lang.IllegalArgumentException: No enum constant org.testTask.DTO.Publisher.ПИТЕР
I can't understand what the problem is.

答案1

得分: 4

valueOf 并不起作用。您必须向它提供其中一个枚举成员的名称,即 valueOf("MOSCOW")

您称之为 'name' 的东西并不是枚举的 名称。名称是您可以在源代码中编写的内容,比如 Publisher.MOSCOW

为了通过(俄罗斯)名称进行查找,您需要在枚举成员列表中搜索具有匹配(俄罗斯)名称的成员。如果超过3个成员,使用映射可能会提高效率,但是对于少量的发布者来说可能没有必要。

英文:

valueOf does not work that way. You have to given it the name of one of the enum members, i.e., valueOf("MOSCOW").

What you call 'name' is not the name of the enum. The name is the thing you can write in source code, Publisher.MOSCOW.

To facilitate lookups by (Russian) name, you need to search through the list of enum members for one with a matching (Russian) name. If you have many more than 3, a map would be useful for efficiency, but I would not bother for a small number of publishers.

答案2

得分: 1

.valueOf 方法返回指定枚举类型中具有指定名称的枚举常量,表示 .valueOf("ST_PETERSBURG") 不是来自字段 name

您需要将枚举常量的 name 字段与数据库值进行匹配,并获取枚举值。

protected Publisher getObject(ResultSet resultSet) throws SQLException {
    String enumName = resultSet.getString("NAME");
    return Arrays.stream(Publisher.values())
                 .filter(e -> e.getName().equals(enumName))
                 .findFirst().orElse(null);
}
英文:

.valueOf method returns the enum constant of the specified enumtype with the specified name, means .valueOf("ST_PETERSBURG") not from the field name.

You need to match the name field of enum constants with database value and get the enum

protected Publisher getObject(ResultSet resultSet) throws SQLException {
    String enumName = resultSet.getString("NAME")
    return  Arrays.stream(Publisher.values())
                            .filter(e -> e.getName().equals(enumName))
                            .findFirst().orElse(null);
}

答案3

得分: 0

你的枚举成员是英文名称,不是俄文名称。
你应该将 MOSCOWST_PETERSBURG 或者 O_REILLY 传递给 valueOf()

英文:

Your Enum members are the names in English, not the ones in Russian.
you should pass MOSCOW, ST_PETERSBURG or O_REILLY to valueOf()

huangapple
  • 本文由 发表于 2020年8月16日 03:41:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63430161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定