1

我想通过使用枚举类型之一作为输入来设置枚举类型:

这是我正在使用的代码,

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

当我尝试使用new Item("A")它时,它会返回给我一个NullPointerException occured : Name is null

4

1 回答 1

1

试试这个:

public Item(String itemType) {
   if (itemType == null) {
       throw new IllegalArgumentException("null itemType");
   }
   this.itemType = Type.valueOf(itemType);
}
于 2011-05-30T22:02:44.227 回答