groovy - Setting a class property to an Enum value -
i have abstract groovy class have attribute of type enum. however, i'm not clear on how set up.
i have this:
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 }
and enum looks :
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} }
so typically sort of nullpointerexception cannot set value null object. not sure i'm missing, or if @ possible.
java.lang.nullpointerexception: cannot set property 'value' on null object @ org.codehaus.groovy.runtime.nullobject.setproperty(nullobject.java:66) @ org.codehaus.groovy.runtime.invokerhelper.setproperty(invokerhelper.java:192) @ org.codehaus.groovy.runtime.scriptbytecodeadapter.setproperty(scriptbytecodeadapter.java:480) @ ***.****.***.****.<init>(**.groovy:30) @ ****.***.***.****.<init>(***.groovy:10) @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(unknown source) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(unknown source) @ java.lang.reflect.constructor.newinstance(unknown source) @ org.codehaus.groovy.reflection.cachedconstructor.invoke(cachedconstructor.java:77) @ org.codehaus.groovy.runtime.callsite.constructorsite$constructorsitenounwrapnocoerce.callconstructor(constructorsite.java:102) @ org.codehaus.groovy.runtime.callsite.callsitearray.defaultcallconstructor(callsitearray.java:57) @ org.codehaus.groovy.runtime.callsite.abstractcallsite.callconstructor(abstractcallsite.java:182) @ buchwalter.oldschoolgame.characters.fightertest.getfullconcrete(fightertest.groovy:18) @ buchwalter.oldschoolgame.characters.playertest.shouldreturngendertype(playertest.groovy:114) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:45) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:15) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:42) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:20) @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:28) @ org.junit.runners.parentrunner.runleaf(parentrunner.java:263) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:68) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:47) @ org.junit.runners.parentrunner$3.run(parentrunner.java:231) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:60) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:229) @ org.junit.runners.parentrunner.access$000(parentrunner.java:50) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:222) @ org.junit.runners.parentrunner.run(parentrunner.java:300) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:467) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:683) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:390) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:197)
the problem in person
constructor. can't set attribute of enumeration. pass enumeration object constructor or pass integer value
attribute , find
enumeration object has value. i've cleaned example, should work when add remaining constructor parameters:
// won't work because 'this.occupation' null protected person(int ocp) { this.occupation.value = ocp }
so either pass enumeration object:
protected person(occupationtype type) { this.occupation = type }
or receive int attribute , find
it:
protected person(int ocp) { this.occupation = occupationtype.find { it.value == ocp } }
Comments
Post a Comment