将类属性设置为枚举值

时间:2013-05-18 20:24:24

标签: groovy enums grails-domain-class

我有一个抽象的groovy类,我想拥有一个Enum类型的属性。但是,我不清楚如何设置它。

我有这个:

abstract class Person { 
protected int heightFeet, heightInches, weight
protected String firstName, lastName
protected OccupationType occupation

protected Person(int hf, hi, w, string fn, string ln, int ocp){
        this.heightFeet = hf
this.heightInches = hi
this.weight = w
this.firstName = fn
this.lastName = ln
this.occupation.value = ocp
}

和Enum看起来像这样:

public enum OccupationType {
   Teacher(1), Administrator(2), Counselor(3), Doctor(4), Nurse(5), ...
 OccupationsType(int value) {this.value = value}
 private final int value
 public int value() {return value}
}

所以我通常会得到某种NullPointerException,它无法将值设置为null对象。不确定我缺少什么,或者这是否可能。

java.lang.NullPointerException: Cannot set property 'value' on null object
    at org.codehaus.groovy.runtime.NullObject.setProperty(NullObject.java:66)
    at org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:192)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:480)
    at ***.****.***.****.<init>(**.groovy:30)
    at ****.***.***.****.<init>(***.groovy:10)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
    at buchwalter.oldschoolgame.characters.FighterTest.getFullConcrete(FighterTest.groovy:18)
    at buchwalter.oldschoolgame.characters.PlayerTest.shouldReturnGenderType(PlayerTest.groovy:114)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

1 个答案:

答案 0 :(得分:0)

问题出在您的Person构造函数中。您无法设置枚举的属性。您只需将枚举对象传递给构造函数,或者传递整数value属性,并传递具有该值的枚举对象find。我已经清理了你的例子,但是当你添加剩下的构造函数参数时它应该可以工作:

// this won't work because 'this.occupation' IS null
protected Person(int ocp) {
  this.occupation.value = ocp
}

因此要么传递枚举对象:

protected Person(OccupationType type) {
  this.occupation = type
}

OR接收一个int属性并find

protected Person(int ocp) {
  this.occupation = OccupationType.find { it.value == ocp }
}