Menu

#138 CSVMalformedException when a string ("null") assigned to a integer/double

closed
None
5
2020-11-20
2020-09-29
Nandagopal
No

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.

Discussion

  • Scott Conway

    Scott Conway - 2020-09-29

    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.

    @DisplayName("Parse a Bean that has a null for a primitive value.")
    @Test
    public void parseBeanThatHasNullForInt() {
        String testString = "name,num,orderNumber\n" +
                "kyle,123,abc123456\n" +
                "jimmy,,def098765";
    
        HeaderColumnNameMappingStrategy<MockBean> strategy = new HeaderColumnNameMappingStrategy<>();
        strategy.setType(MockBean.class);
        List<MockBean> beanList = new CsvToBeanBuilder<MockBean>(new StringReader(testString))
                .withMappingStrategy(strategy)
                .build().parse(); // Extra arguments for code coverage
    
        assertEquals(2, beanList.size());
        assertTrue(beanList.contains(new MockBean("kyle", null, "abc123456", 123, 0.0)));
        assertTrue(beanList.contains(new MockBean("jimmy", null, "def098765", 0, 0.0)));
    }
    
     
  • Scott Conway

    Scott Conway - 2020-10-18

    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.

    @DisplayName("Parse a Bean that has the word null for a primitive value.")
    @Test
    public void parseBeanThatHasNullForInt() {
        String testString = "name,num,orderNumber\n" +
                "kyle,123,abc123456\n" +
                "jimmy,null,def098765";
    
        HeaderColumnNameMappingStrategy<FR138MockBean> strategy = new HeaderColumnNameMappingStrategy<>();
        strategy.setType(FR138MockBean.class);
        List<FR138MockBean> beanList = new CsvToBeanBuilder<FR138MockBean>(new StringReader(testString))
                .withMappingStrategy(strategy)
                .build().parse(); // Extra arguments for code coverage
    
        assertEquals(2, beanList.size());
        assertTrue(beanList.contains(new FR138MockBean("kyle", null, "abc123456", 123, 0.0)));
        assertTrue(beanList.contains(new FR138MockBean("jimmy", null, "def098765", 0, 0.0)));
    }
    

    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.

    @PreAssignmentProcessor(processor = ConvertWordNullToNull.class)
    private int num;
    

    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 :)

     
  • Andrew Rucker Jones

    • assigned_to: Andrew Rucker Jones --> Scott Conway
     
  • Scott Conway

    Scott Conway - 2020-11-20

    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.

     
  • Scott Conway

    Scott Conway - 2020-11-20
    • status: open --> closed
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.