Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
NullPointerException in StringToCollectionConverter.convertToPresentation
I am using StringToCollectionConverter, to convert between a List<String> 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):
public String convertToPresentation(Collection value,
Class<? extends String> targetType, Locale locale)
throws Converter.ConversionException {
......
for (Iterator<?> iterator = value.iterator(); iterator.hasNext();) {
......
}
......
}
There is no null check for value.
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.
Is this a bug?
Hi Ying,
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<String> myEmptyList = new ArrayList<String>();
or
String myEmptyString = "";
for example?
Best,
Enver
Hi Enver,
Thanks for your answer.
I will set a default empty list value for my property, to avoid this NullPointerException.
But I still don't understand:
The other Converters doing this check.
For example,
in method AbstractStringToNumberConverter.convertToPresentation(T value,
Class<? extends String> targetType, Locale locale)
throws ConversionException {
if (value == null) {
return null;
}
......
}
and in method AbstractStringToNumberConverter. convertToNumber(String value,
Class<? extends Number> targetType, Locale locale)
throws ConversionException {
if (value == null) {
return null;
}
......
}
both doing null value check.
And in StringToDateConverter, it also doing null value check.
Looks like StringToCollectionConverter is the only one which not doing null value check.
Why?
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'.
Thanks for helping making Vaadin better,
--Enver
Hi Enver,
Thanks for your answer.
I created a ticket for this problem:
https://dev.vaadin.com/ticket/19481
Please check it. Thanks very much
Thank you!
We're working on it.
https://dev.vaadin.com/review/#/c/12897/
Thank you again for pointing it out.
Best,
--Enver