I am using StringToCollectionConverter, to convert between a List property and a TextField.
But the StringToCollectionConverter throw a NullPointerException, because my property is null.
In the source code of StringToCollectionConverter.convertToPresentation (ver 7.6.1):
And also, In the source code of StringToCollectionConverter.convertToModel:
public Collection convertToModel(String value,
Class<? extends Collection> targetType, Locale locale)
throws Converter.ConversionException {
int index = value.indexOf(delimiter);
......
}
There is no null check for value, too.
this is not a bug - it’s good practice to not check for null, in order to then throw ConversionException or RuntimeException or yet another NullPointerException.
Quite obviously, for the two methods to work in a sensible fashion, the ‘value’ must be non-null; and the NullPointerException is signalling exactly that this precondition was violated.
If you want to use an empty collection or an empty String, why not use
List myEmptyList = new ArrayList();
or
String myEmptyString = “”;
for example?
You are right in that this is somewhat inconsistent.
We never really specified what should happen in a Converter when it is asked to convert a NULL.
Now that you spotted that one converter behaves different from the others in a non-specified corner
case I invite you to write a ticket on this so we can take care of it: https://dev.vaadin.com/
Please mark it as ‘enhancement’.