Spring Batch ItemReader 在第一行获取 NullPointer

当我尝试读取我的 csv 文件时,我遇到了 Spring batch 的这个问题。


我必须阅读具有如下结构的CSV :


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

我有一个MultiResourceItemReader,它委托一个String 类型的FlatFileItemReader,以便轻松地将此行映射到我在之后执行的自定义项处理器中的自定义 DTO。


在我的 Tasklet 中,我像这样注入了我的 MultiResourceItemReader bean:


@Autowired

    private MultiResourceItemReader<String> resourceItemReader;

然后我这样称呼它(设置资源和上下文):


    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



不负相思意
浏览 97回答 1
1回答

繁星点点滴滴

您没有在您的LineMapper. 这是添加一个的方法:&nbsp; &nbsp; reader.setLineMapper(new DefaultLineMapper<String>() {&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Setea los valores en la clase indicada&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setFieldSetMapper(new FieldSetMapper<String>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public String mapFieldSet(FieldSet fieldSet) throws BindException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] values = fieldSet.getValues();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return String.join(";", values);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setLineTokenizer(new DelimitedLineTokenizer(";"));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });FlatFileItemReaderBuilder由于其流畅的 API,我建议使用 which更直观。这是一个例子:@Beanpublic FlatFileItemReader<Pojo> flatFileItemReader() {&nbsp; &nbsp; return new FlatFileItemReaderBuilder<Pojo>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .name("itemReader")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .resource(new FileSystemResource("/path/to/flat/file"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .targetType(Pojo.class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .delimited()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .delimiter(";")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .names("column1", "column2")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java