CSVMalformedException when a string ("null") assigned to a integer/double
Brought to you by:
aruckerjones,
sconway
Hey Team, OpenCSV is working on awesome way. but i stuck on the following error,
CSV data sample:
name age description
nanda 20 test123
nanda null test123
nanda null test123
nanda null test123
On My Model:
Class User {
private String name;
private int age;
}
Error:
CSVMalformedException returned by openCSV. Is there any way to filter "null" string before parsing?. kindly help me on this.
What version of opencsv are you using and can you give a code example. I wrote the following test on the latest version of opencsv and it passes with 0 being assigned to the int.
Hello Nandagopal
Sorry I was just about to close this for lack of response and took a closer look at the error and read the header and realized you were physically passing in the word null in your csv file.
Well don't do that!
Just kidding (really though don't do that if you can avoid it) but I rewrote the test and got a similar error. The fact that the error is different leads me to believe that you are using an older version of opencsv.
Note I changed MockBean to FR138MockBean. That was because I had to change the class to make this work.
You need to be using opencsv 5.0 or greater. In that we added processor and validator classes that allow you to test/manipulate the data before assignment to the bean. You can see more of this at http://opencsv.sourceforge.net/#stringprocessor_and_preassignmentprocessor
So I created a processor that checked if the field was the string "null" and if so replaced it with an actual null string. Then in the FR138MockBean class I added the PreAssignmentProcessor annotation to the desired field.
and after that the test passed.
I pushed the actual code into the main branch so you can clone the repo and look at src/test/java/integrationTest/FR138 to see all the classes and the test.
Hope that helped.
Scott :)
I am closing this for inactivity but for the 5.4 release I have moved the StringProcessors I had created for unit and integration tests out to com.opencsv.bean.processor. Including the ConvertWordNullToNull.