java.lang.非法参数异常:无枚举常量

y3bcpkx1  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(190)

我的枚举有问题。
这就是:

public enum DataEnum {
    NAME_PEOPLE("NAME_PEOPLE"),
    FIRSTNAME_PEOPLE("FIRSTNAME_PEOPLE"),
    ID("ID"),
    PASS("PASS"),
    NEW_MAIL("NEW_MAIL");

    private  String name;
    private DataEnum(String s) {
        name = s;
    }
    public String getValue() {
        return name;
    }
    public void setValue(String s) {
        this.name = s;
    }
}

我在这里使用它:

public String transform(String textToTransform, People people){
        Pattern TAG_REGEX = Pattern.compile("#(.+?)#");
        Matcher matcher = TAG_REGEX.matcher(textToTransform);
        while (matcher.find()) {
            String s = matcher.group(1);
            switch (s) {
                case "FIRSTNAME_PEOPLE":
                    DataEnum.valueOf(s).setValue(people.getFirstName());
                    break;
                case "NAME_PEOPLE":
                    DataEnum.valueOf(s).setValue(people.getName());
                    break;
                case "ID":
                    DataEnum.valueOf(s).setValue(people.getEmail());
                    break;
                case "PASS":
                    DataEnum.valueOf(s).setValue(people.getPassword());
                    break;
                default:
                    break;
            }
            textToTransform = textToTransform.replace("#" + DataEnum.valueOf(s) + "#", DataEnum.valueOf(s).getValue());
        }
        return textToTransform;
    }

我得到了以下错误:
原因:java.lang.非法参数异常:无枚举常量fr.pdf.utils.DataEnum.FIRSTNAME_PEOPLE
编辑:
原因:java.lang.非法参数异常:无枚举常量fr.pdf.utils.DataEnum.FIRSTNAME_PEOPLE at java.lang.Enum.valueOf(Enum.java:238)at fr.pdf.utils.DataEnum.valueOf(DataEnum.java:3)at fr.pdf.services.impl.MailServiceImpl.transform(MailServiceImpl. java:160)at fr.pdf.services.impl.MailServiceImpl.sendMail(MailServiceImpl. java:84)at fr.pdf.dao.impl.People.update(People.java:372)
行160对应于:

textToTransform = textToTransform.replace("#" + DataEnum.valueOf(s) + "#", DataEnum.valueOf(s).getValue());
j8ag8udp

j8ag8udp1#

删除枚举并执行以下操作:)

public String transform(String textToTransform, People people){
    Pattern TAG_REGEX = Pattern.compile("#(.+?)#");
    Matcher matcher = TAG_REGEX.matcher(textToTransform);
    while (matcher.find()) {
        String s = matcher.group(1);
        String replaceWith = null;
        switch (s) {
            case "FIRSTNAME_PEOPLE":
                replaceWith = people.getFirstName();
                break;
            case "NAME_PEOPLE":
                replaceWith = people.getName();
                break;
            case "ID":
                replaceWith = people.getEmail();
                break;
            case "PASS":
                replaceWith = people.getPassword();
                break;
            default:
                break;
        }
        textToTransform = textToTransform.replace("#" + s + "#", replaceWith);
    }
    return textToTransform;
}

相关问题