-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StringToIntegerConverter makes TextField throw NullPointerException on null Integer value #6746
Comments
Simple code: public class IntHolder {
private Integer myvalue;
// + getter/setter
}
binder = new Binder(IntHolder.class);
binder.forField(new TextField()).withConverter(new StringToIntegerConverter("Not an integer")).bind("myvalue");
binder.readBean(new IntHolder()); fails with
|
Workaround: patch the Converter to return an empty string instead of null: public class StringToIntegerConverter2 extends StringToIntegerConverter {
public StringToIntegerConverter2() {
super("Not an integer");
}
@Override
public String convertToPresentation(Integer value, ValueContext context) {
final String result = super.convertToPresentation(value, context);
return result == null ? "" : result;
}
} |
You're supposed to define an explicit |
@Legioth not true, broken already by Binder since it uses Making the simplest conversion code fail with NPE is bad dev experience. Forcing the dev to always write If you hate accepting nulls for TextFields and you want to preserve Converters to pass nulls, then at least consider failing with a different exception, e.g. |
The problem is that it's also a potentially bad experience to do "magic" things on the developer's behalf. Just ask anyone who has ever used any dependency injection framework :) Good point about the automatic null representation in cases when there isn't any converter at all. The idea there is that defining a converter puts the binding in "manual" mode without any magic at all whereas simple bindings without any explicit conversion logic can be slightly magic. You are right that we could at the very least somehow improve the error message for this particular case. I'm not 100% sure how to do that since We could also consider whether there would be some alternative way of defining explicit null conversion with less boilerplate, e.g. as an optional parameter to the converter constructor. This would on the other hand have a negative impact on code readability. |
For reference, there are also related discussions for the identical functionality in Vaadin 8: vaadin/framework#8664 & vaadin/framework#11109 |
@Legioth very good point with magic+DI, I avoid DI like a plague.
That sounds really good. Rethrowing as RuntimeException changes exception type which should be avoided. Rethrowing only in special case as described above sounds good to me, and navigates the dev to the correct solution.
Yeah, StringToIntegerConverter already has 4 constructors and with optional boolean param there would be too many. We could add |
A binding doesn't have an automatic null representation configured if a converter is also defined. This may cause surprises with fields that don't accept null values, e.g. TextField. The easiest fix in this situation is to explicitly define a null representation. This patch adds an error message that reminds the developer about the existence of that feature so that they wouldn't have to make more elaborate workarounds. Fixes #6746
#6757 adds a mention of null representations to the exception message. |
A binding doesn't have an automatic null representation configured if a converter is also defined. This may cause surprises with fields that don't accept null values, e.g. TextField. The easiest fix in this situation is to explicitly define a null representation. This patch adds an error message that reminds the developer about the existence of that feature so that they wouldn't have to make more elaborate workarounds. Fixes #6746
A binding doesn't have an automatic null representation configured if a converter is also defined. This may cause surprises with fields that don't accept null values, e.g. TextField. The easiest fix in this situation is to explicitly define a null representation. This patch adds an error message that reminds the developer about the existence of that feature so that they wouldn't have to make more elaborate workarounds. Fixes #6746 (cherry picked from commit 3e30650)
A binding doesn't have an automatic null representation configured if a converter is also defined. This may cause surprises with fields that don't accept null values, e.g. TextField. The easiest fix in this situation is to explicitly define a null representation. This patch adds an error message that reminds the developer about the existence of that feature so that they wouldn't have to make more elaborate workarounds. Fixes #6746 (cherry picked from commit 3e30650)
A binding doesn't have an automatic null representation configured if a converter is also defined. This may cause surprises with fields that don't accept null values, e.g. TextField. The easiest fix in this situation is to explicitly define a null representation. This patch adds an error message that reminds the developer about the existence of that feature so that they wouldn't have to make more elaborate workarounds. Fixes #6746 (cherry picked from commit 3e30650)
A binding doesn't have an automatic null representation configured if a converter is also defined. This may cause surprises with fields that don't accept null values, e.g. TextField. The easiest fix in this situation is to explicitly define a null representation. This patch adds an error message that reminds the developer about the existence of that feature so that they wouldn't have to make more elaborate workarounds. Fixes #6746 (cherry picked from commit 3e30650)
Vaadin 14.0.9. Binding a
null
Integer
field to aTextField
viaStringToIntegerConverter
fails with NPE. This is the most basic use-case which simply must work out of the box.The text was updated successfully, but these errors were encountered: