In my Vaadin (v.23.2.6) application I have a form tied up to Filter class which has 5 attributes. All of them are optional, i.e. user can leave the blank.
public FilterPanel(ApiBookUtils api) {
this.api = api;
this.authorField = new ComboBox<Author>("Author Name");
this.countryField = new ComboBox<>("Country");
this.countryField.setReadOnly(true);
this.fromYear = new IntegerField ("From");
this.fromYear.setWidth("60px");
this.toYear = new IntegerField ("To");
this.toYear.setWidth("60px");
this.binder = new Binder(Filter.class);
this.setModal(true);
this.setCloseOnOutsideClick(false);
this.setCloseOnEsc(true);
buildDialog();
}
private void buildDialog() {
bindFields();
addFields();
setDialogListeners();
setDialogItems();
}
private void bindFields() {
this.binder.bind(authorField, Filter::getAuthor, Filter::setAuthor);
this.binder.forField(countryField).bind(Filter::getCountry, Filter::setCountry);
this.binder.forField(fromYear).bind(Filter::getFromYear, Filter::setFromYear);
this.binder.forField(toYear).bind(Filter::getToYear, Filter::setToYear);
this.binder.forField(postingDateField).bind(Filter::getPostingDate, Filter::setPostingDate);
this.binder.forField(tagField).bind(Filter::getTags, Filter::setTags);
}
I am getting getting exception if IntegerField is left blank.
com.vaadin.flow.data.binder.BindingException: An exception has been thrown inside binding logic for the field element [label='From'] at com.vaadin.flow.data.binder.Binder$BindingImpl.execute(Binder.java:1570) ~[flow-data-23.2.5.jar:23.2.5] at com.vaadin.flow.data.binder.Binder$BindingImpl.writeFieldValue(Binder.java:1427) ~[flow-data-23.2.5.jar:23.2.5] at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na] Caused by: java.lang.NullPointerException: null at com.vaadin.flow.data.binder.Binder$BindingImpl.lambda$writeFieldValue$5169480d$1(Binder.java:1431) ~[flow-data-23.2.5.jar:23.2.5]
Does anybody know how to make binder to accept empty field and set up default value in the bean?
I found the workaround the bug in the Binder. Apparently, it does not process primitive types correctly. I have replaced int fields in my bean with Integer object and exception was gone.