[英]How to extract all values from an enum an add them to a List<String>
The title is pretty self-explanatory. 标题很不言自明。 Here's my code so far:
到目前为止,这是我的代码:
public List<String> getVoucherStatuses() {
List<String> listOfStatuses = new ArrayList<String>();
for (VoucherStatus status : VoucherStatus.values()) {
listOfStatuses.add(status.name());
}
return listOfStatuses;
}
and here is the Enum: 这是枚举:
public enum VoucherStatus {
GENERATED, INVALID, ISSUED, REDEEMED, EXPIRED
}
I keep getting null reference expcetion. 我不断收到空引用期望。 What am I doing wrong?
我究竟做错了什么? Thanks
谢谢
If you're calling this from within the static initialiser of the enum, it wont have finished initialising which may well give you an NPE. 如果您是从枚举的静态初始化程序中调用此函数,则它将不会完成初始化,这很可能会给您带来NPE。
One work around is to use a nested class for the static. 解决方法之一是对静态使用嵌套类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.