首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置枚举的字符串值

设置枚举的字符串值
EN

Stack Overflow用户
提问于 2011-05-31 05:55:31
回答 1查看 2.2K关注 0票数 1

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

这就是我使用的代码,

代码语言:javascript
复制
    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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-31 06:02:44

试试这个:

代码语言:javascript
复制
public Item(String itemType) {
   if (itemType == null) {
       throw new IllegalArgumentException("null itemType");
   }
   this.itemType = Type.valueOf(itemType);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6181054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档