javaspringspring-batchspring-batch-tasklet

Spring Batch ItemReader gets NullPointerException in first line


I have this problem with Spring batch when I try read my csv files.

I have to read a CSV with a structure like this:

valueHeader;valueHeader;valueHeader;valueHeader;valueHeader;valueHeader
conventValue;conventValue;conventValue;conventValue;conventValue
conventValue;conventValue;conventValue;conventValue;conventValue
conventValue;conventValue;conventValue;conventValue;conventValue
valueFooter;valueFooter;valueFooter;valueFooter;valueFooter;valueFooter

I have a MultiResourceItemReader that delegate on a FlatFileItemReader of type String, to make easy map this lines to my Custom DTO in a Custom Item Processor that is executed after.

In my Tasklet, I have injected my MultiResourceItemReader bean like this:

@Autowired
    private MultiResourceItemReader<String> resourceItemReader;

Then I call it like this (set the resources and context):

    resourceItemReader.setStrict(Boolean.FALSE);
    resourceItemReader.setResources(resources);
    resourceItemReader.open(context.getStepContext().getStepExecution().getExecutionContext());

    String currentContent = resourceItemReader.read();
    while(currentContent != null) {
        linesReaded.add(currentContent);
        currentContent = resourceItemReader.read();
    }

But I am getting a **NullPointerException** in the first Line and I don't know why:

20190705 19:53:30.041 INFO   com.mycomp.mycustomp.tasklet.MyCustomTasklet [[ # ]] - [MyCustomTasklet:EXECUTE]
20190705 19:53:41.560 ERROR  org.springframework.batch.core.step.AbstractStep [[ # ]] - Encountered an error executing step processRebateASLIStep in job MyProyectJob
org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 1 in resource=[file [C:\Pruebas\MyProyectJob\destino\MyFile-100-20151130143000-000001.csv]], input=[HEADER;PEP;FacteTRY;100; 20151130143000;000125;100000000000000000000000000000000025874]
    at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:183)
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88)
    at org.springframework.batch.item.file.MultiResourceItemReader.readFromDelegate(MultiResourceItemReader.java:140)
    at org.springframework.batch.item.file.MultiResourceItemReader.readNextItem(MultiResourceItemReader.java:119)
    at org.springframework.batch.item.file.MultiResourceItemReader.read(MultiResourceItemReader.java:108)
....
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.NullPointerException: null
    at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:43)
    at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:180)
    ... 59 common frames omitted

I will attach my readerItems beans too:

@Bean
        MultiResourceItemReader<String> resourceItemReader(FlatFileItemReader<String> flatFileItemReader){
            MultiResourceItemReader<String> resourceItemReader = new MultiResourceItemReader<>();
            resourceItemReader.setDelegate(flatFileItemReader);
            resourceItemReader.setStrict(false);
            return resourceItemReader;
        }

    @Bean
    FlatFileItemReader<String> flatFileItemReader() {

        FlatFileItemReader<String> reader = new FlatFileItemReader<String>();
        reader.setStrict(false);
        reader.setLineMapper(new DefaultLineMapper<String>() {
        {
            //Setea los valores en la clase indicada
            setFieldSetMapper(new FieldSetMapper<String>() {

                @Override
                public String mapFieldSet(FieldSet fieldSet) throws BindException {
                    String[] values = fieldSet.getValues();
                    return String.join(";", values);
                }

            });

            }
        });

        return reader;
    }

Solution

  • You did not set a line tokenizer on your LineMapper. Here is how to add one:

        reader.setLineMapper(new DefaultLineMapper<String>() {
            {
                //Setea los valores en la clase indicada
                setFieldSetMapper(new FieldSetMapper<String>() {
    
                    @Override
                    public String mapFieldSet(FieldSet fieldSet) throws BindException {
                        String[] values = fieldSet.getValues();
                        return String.join(";", values);
                    }
    
                });
    
                setLineTokenizer(new DelimitedLineTokenizer(";"));
    
            }
        });
    

    I would recommend using the FlatFileItemReaderBuilder which is more intuitive thanks to its fluent API. Here is an example:

    @Bean
    public FlatFileItemReader<Pojo> flatFileItemReader() {
        return new FlatFileItemReaderBuilder<Pojo>()
                .name("itemReader")
                .resource(new FileSystemResource("/path/to/flat/file"))
                .targetType(Pojo.class)
                .delimited()
                .delimiter(";")
                .names("column1", "column2")
                .build();
    }