String Value to set an Enum
I would like to set an enum type b开发者_StackOverflowy using one of its values as an input :
This is the code i'm using,
package models;
import models.crudsiena.SienaSupport;
import siena.*;
public class Item extends SienaSupport {
@Id
public Long id;
public static enum Type{
A,
B
};
public Type itemType;
public Item(String itemType) {
this.itemType = Type.valueOf(itemType);
}
}
When I try to use new Item("A")
it returns me a NullPointerException occured : Name is null
Try this:
public Item(String itemType) {
if (itemType == null) {
throw new IllegalArgumentException("null itemType");
}
this.itemType = Type.valueOf(itemType);
}
精彩评论