开发者

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜