1

我有一个抽象的 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
}

枚举看起来像这样:

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 它不能将值设置为空对象。不知道我错过了什么,或者这是否可能。

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)
4

1 回答 1

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
}

或接收一个 int 属性,find它:

protected Person(int ocp) {
  this.occupation = OccupationType.find { it.value == ocp }
}
于 2013-05-19T13:22:33.233 回答