I have a strange issue with my CheckBox bound with a Binder.
I load my bean from the DB and it has a property readyToOrder
which is false
.
But directly after i bind the bean, using binder.setBean(myBean);
, the value of readyToOrder
is null
- which causes Exceptions in other parts of my application
I have tracked it down to the initFieldValue method, more exactly in the conversion to model. But I am not really sure what causes the misconversion (edit: it’s the nullRepresentation of the binding somehow).
I am using Vaadin 12.0.2
Here are the relevant code pieces:
// Bean Class
@Entity
public class BeanClass {
private Boolean readyToOrder = false;
public BeanClass(){
}
private Boolean isReadyToOrder(){
return readyToOrder;
}
private void setReadyToOrder(Boolean readyToOrder){
this.readyToOrder = readyToOrder;
}
}
// CheckBox binding in view
Binder<BeanClass> binder = new Binder<>();
CheckBox checkBox = new CheckBox("Ready To Order");
checkBox.setWidth("100%");
binder.forField(checkBox)
.withNullRepresentation(false)
.bind(BeanClass::isReadyToOrder, BeanClass::setReadyToOrder);
BeanClass myBean = new BeanClass();
// myBean.readyToOrder: false
binder.setBean(myBean);
// myBean.readyToOrder: null
[screenshot: debugging of binder.setBean()]
(https://imgur.com/a/Popt4qj)
I just solved it while writing this question, haha! I was able to fix it by removing .withNullRepresentation(false);
from the field binding. If the value was null before binder.setBean()
, it is now false. If the value was false before, it stays false. This is exactly what I wanted to achieve with .withNullRepresentation(false) but apparently that was not necessary. In the contrary: it made false
become null in the actual bean, instead of null “becoming” false
for presentation. Why I do not know. I only thought of setting a nullRepresentation in the first place because readyToOrder is of type Boolean and not a primitive boolean.
To sum up: I have no question to you anymore. You don’t have to answer me. I finished writing this question only to make you guys aware of this if this is a bug (which I am not sure of). I just feel like I have wasted too much time on this to not share my solution
Thank you