The strange thing is that my Kotlin code compiled fine before, when it looked like this in the java class Allocator
:
public void setAllocMethod(@NotNull AllocMethod allocMethod) {
this.allocMethod = allocMethod;
}
but when I changed the java class' setter to this:
public void setAllocMethod(@Nullable AllocMethod allocMethod) {
this.allocMethod= allocMethod;
}
then when I compile the project, I get this Kotlin error in the kt file that calls the java object:
Val Cannot be Reassigned
allocator.allocMethod = DefaultAllocMethod() // kotlin code
also here is the java getter:
public @NotNull AllocMethod getAllocMethod() {
if (allocMethod == null) allocMethod = DefaultAllocMethod.newDefault();
return allocMethod;
}
DefaultAllocMethod
is a java subclass of AllocMethod
allocator
is of type Allocator
, which is a java class that has the getter and setter described above.
Can anyone explain what is happening? thanks
Your setters's type @Nullable AllocMethod
, which is Kotlin's AllocMethod?
, does not match the getters type @NotNull AllocMethod
, which is Kotlin's AllocMethod
What the error message means is that since the types do not match, only the getter is considered as a property. So from the Kotlin's point of view instead of a var allocMethod
you have val allocMethod
and fun setAllocMethod(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With