Menu

#158 CsvToBeanBuilder - empty field always null?

v1.0 (example)
closed-fixed
None
5
2017-10-09
2017-10-04
Fabian T.
No

Hi,

Please have a look at the following stackoverflow thread: https://stackoverflow.com/questions/42856200/opencsv-csvreadernullfieldindicator-seems-to-make-no-difference

I got the same problem here. But let me give you more details, maybe my code is wrong ;-)

Versions: I just migrated from opencsv 2.3 to 4.0

In this case we’re trying to import a small tab separated csv file with a small amount of columns.

With the old Version 2.3:
“Column A\tColumn B\tColumn C\n”
“Data A \tData B \t\n
Result after the conversion => Column C of the bean got the value “”

Second example:
“Column A\tColumn B\tColumn C\n”
“Data A \tData B\n ==> the \t after Data B is missing
Result after the conversion => Column C of the bean was null. Since \t was missing in the import I think it’s ok.

Now with Version 4 we got always a null as result. Seems to be the same problem as described at stackoverflow.

Let’s analyse the code! On the class “CsvToBeanBuilder” it’s possible to determine in which case a field is going to be null or not => .withFieldAsNull(CSVReaderNullFieldIndicator….)

CSVReaderNullFieldIndicator value is used in CVSParser#shouldConvertEmptyToNull. When parsing an empty column, the Method “convertEmptyToNullIfNeeded” return, as I wish an empty String. So everything is correct there.

The problem seems to be in the BeanFieldPrimitiveTypes#convert method. The value is still an empty String, but with the “isNotBlank” check (which is true for an empty String) the method returns null.

Is this a bug or not? If not, how am I supposed to set an empty String in my bean instead of null? And what is CSVReaderNullFieldIndicator used for in that case?

Thx

Discussion

  • Scott Conway

    Scott Conway - 2017-10-06

    This is going to require some thought as the issue is in BeanFieldPrimitiveTypes class which will only set a value if there is a value (so empty fields will result in a null). This is because this is is converting to all types and most do not handle empty strings (Integer). So this class needs to be modified to check the type of the field and if it is a certain set of types (String for now) then allow an empty value to be converted.

     
  • Scott Conway

    Scott Conway - 2017-10-08
    • assigned_to: Scott Conway
     
  • Scott Conway

    Scott Conway - 2017-10-09
    • status: open --> closed-fixed
     
  • Scott Conway

    Scott Conway - 2017-10-09

    The fix has been merged in and will be in the 4.1 release of opencsv.

     
  • Fabian T.

    Fabian T. - 2017-10-09

    Thx Scott (Y)

     

Log in to post a comment.

MongoDB Logo MongoDB