'Custom converter throws java.lang.ClassCastException when calling setBean on binder for field type of textfield
Trying to implement a custom converter for a textfield to a custom data type and bonded the same field with the binder when I call binder.setBean(vo) it throws the given exception
java.lang.ClassCastException: class com.demo.vo.DemoVO cannot be cast to class java.lang.String (com.demo.vo.DemoVO is in unnamed module of loader 'deployment.demo.ear' @7cc3d734; java.lang.String is in module java.base of loader 'bootstrap') at deployment.demo.ear.demo.war//com.vaadin.flow.component.textfield.TextField.setValue(TextField.java:34) at deployment.demo.ear.demo.war//com.vaadin.flow.data.binder.Binder$BindingImpl.convertAndSetFieldValue(Binder.java:1275) at deployment.demo.ear.demo.war//com.vaadin.flow.data.binder.Binder$BindingImpl.initFieldValue(Binder.java:1196) at deployment.demo.ear.demo.war//com.vaadin.flow.data.binder.Binder$BindingImpl.access$200(Binder.java:1032) at deployment.demo.ear.demo.war//com.vaadin.flow.data.binder.Binder.lambda$setBean$1(Binder.java:1772) at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
DemoVO Class
public class DemoVO {
private int id;
private String code;
private String details;
// getters
// setters
}
Converter Class
public class DemoVOToStringConverter implements Converter<String, DemoVO> {
private DemoVO demoVO;
@Override
public Result<DemoVO> convertToModel(String value, ValueContext context) {
if (null == value)) {
return Result.ok(null);
}
demoVO.setCode(value);
return Result.ok(demoVO);
}
@Override
public String convertToPresentation(DemoVO value, ValueContext context) {
if (null == value)) {
return null;
}
demoVO = value;
return value.getCode();
}
}
HeaderVO Class
public class HeaderVO {
private DemoVO demo = new DemoVO();
// getter
// setter
}
DemoView Class
public class DemoView extends VerticalLayout implements BeforeEnterListener {
private Binder<HeaderVO> binder = null;
public DemoView() {
binder = new Binder<>(HeaderVO.class);
TextField demoField = new TextField();
demoField.setMaxLength(10);
demoField.setLabel("Demo");
binder.forField(demoField).withConverter(new DemoVOToStringConverter()).bind("demo");
add(demoField);
}
@Override
public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
HeaderVO headerVO = new HeaderVO();
binder.setBean(headerVO);
}
}
Solution 1:[1]
While testing your MRE I didn't got a ClassCastException, but I got this exception: 'java.lang.NullPointerException: Null value is not supported'. The TextField does not support a null value, the empty value is a empty string. Note that on your on your convertToPresentation method: never return null.
I would use the bind() method with ValueProvider and Setter:
binder.forField(demoField)
.bind(
header -> header.getDemo().getCode(),
(header, code) -> header.getDemo().setCode(code)
);
Watch out for null values.
(Futher I am not sure about that instance of the DemoVO in the DemoVOToStringConverter... not sure if the Converter could be reused. But looks like it could work.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Leander A. |
