'java.lang.IllegalArgumentException: No enum constant
I have problems with my enum.
Here it is :
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;
}
}
I'm using it there :
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;
}
And I get the following error :
Caused by: java.lang.IllegalArgumentException: No enum constant fr.pdf.utils.DataEnum.FIRSTNAME_PEOPLE
EDIT :
Caused by: java.lang.IllegalArgumentException: No enum constant 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)
Line 160 correspond to :
textToTransform = textToTransform.replace("#" + DataEnum.valueOf(s) + "#", DataEnum.valueOf(s).getValue());
Solution 1:[1]
Drop the enum and do this :)
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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Norbert |
