使用spring批处理和csv文件格式获取空指针异常向db发送数据
Posted
技术标签:
【中文标题】使用spring批处理和csv文件格式获取空指针异常向db发送数据【英文标题】:getting null pointer exception sending data to db using spring batch and csv file format 【发布时间】:2021-10-27 21:06:48 【问题描述】: package com.batch.config;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.LineMapper;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import com.batch.model.User;
@Configuration
@EnableBatchProcessing
public class BatchConfig
@Autowired
private DataSource dataSource;
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuildFactory;
@Bean
public FlatFileItemReader<User> reader()
FlatFileItemReader<User> reader = new FlatFileItemReader<>();
reader.setResource(new ClassPathResource("springbachdata.csv"));
reader.setLineMapper(getlineMapper());
reader.setLinesToSkip(1);
return reader;
private LineMapper<User> getlineMapper()
DefaultLineMapper<User> lineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
lineTokenizer.setNames(new String[] "Emp ID", "Name Prefix", "First Name", "Last Name" );
lineTokenizer.setIncludedFields(new int[] 0, 1, 2, 4 );
BeanWrapperFieldSetMapper<User> fieldSetMapper = new BeanWrapperFieldSetMapper<>();
fieldSetMapper.setTargetType(User.class);
lineMapper.setLineTokenizer(lineTokenizer);
lineMapper.setFieldSetMapper(fieldSetMapper);
return lineMapper;
@Bean
public UserItemProcessor processor()
return new UserItemProcessor();
public JdbcBatchItemWriter<User> writer()
JdbcBatchItemWriter<User> writer = new JdbcBatchItemWriter<>();
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<User>());
writer.setSql(
"insert into user(userId,namePrefix,firstName,lastName) values (:userId, :namePrefix, :firstName, :lastName)");
writer.setDataSource(this.dataSource);
return writer;
@Bean
public Job importUserJob()
return this.jobBuilderFactory.get("USER-IMPORT-JOB")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
@Bean
public Step step1()
return this.stepBuildFactory.get("step1")
.<User, User>chunk(10)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
'java.lang.NullPointerException: 无法调用“org.springframework.batch.item.database.ItemPreparedStatementSetter.setValues(Object, java.sql.PreparedStatement)" 因为 “this.this$0.itemPreparedStatementSetter”在 org.springframework.batch.item.database.JdbcBatchItemWriter$1.doInPreparedStatement(JdbcBatchItemWriter.java:190) ~[spring-batch-infrastructure-4.3.3.jar:4.3.3] 在 org.springframework.batch.item.database.JdbcBatchItemWriter$1.doInPreparedStatement(JdbcBatchItemWriter.java:186) ~[spring-batch-infrastructure-4.3.3.jar:4.3.3] 在 org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:651) ~[spring-jdbc-5.3.9.jar:5.3.9] 在 org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:691) ~[spring-jdbc-5.3.9.jar:5.3.9] 在 org.springframework.batch.item.database.JdbcBatchItemWriter.write(JdbcBatchItemWriter.java:186) ~[spring-batch-infrastructure-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:193) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:159) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:294) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:217) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:77) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:407) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:331) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140) ~[spring-tx-5.3.9.jar:5.3.9] 在 org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:273) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375) ~[spring-batch-infrastructure-4.3.3.jar:4.3.3] 在 org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infrastructure-4.3.3.jar:4.3.3] 在 org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145) ~[spring-batch-infrastructure-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:208) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:152) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:68) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:68) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:137) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:320) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:149) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) ~[spring-core-5.3.9.jar:5.3.9] 在 org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:140) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native 方法)~[na:na] 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na] 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] 在 java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na] 在 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.3.9.jar:5.3.9] 在 org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.3.9.jar:5.3.9] 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.9.jar:5.3.9] 在 org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:128) ~[spring-batch-core-4.3.3.jar:4.3.3] 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.9.jar:5.3.9] 在 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.9.jar:5.3.9] 在 jdk.proxy2/jdk.proxy2.$Proxy58.run(Unknown Source) ~[na:na] at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.execute(JobLauncherApplicationRunner.java:199) ~[spring-boot-autoconfigure-2.5.4.jar:2.5.4] 在 org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.executeLocalJobs(JobLauncherApplicationRunner.java:173) ~[spring-boot-autoconfigure-2.5.4.jar:2.5.4] 在 org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.launchJobFromProperties(JobLauncherApplicationRunner.java:160) ~[spring-boot-autoconfigure-2.5.4.jar:2.5.4] 在 org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.run(JobLauncherApplicationRunner.java:155) ~[spring-boot-autoconfigure-2.5.4.jar:2.5.4] 在 org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.run(JobLauncherApplicationRunner.java:150) ~[spring-boot-autoconfigure-2.5.4.jar:2.5.4] 在 org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:782) ~[spring-boot-2.5.4.jar:2.5.4] 在 org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:772) ~[spring-boot-2.5.4.jar:2.5.4] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:345) ~[spring-boot-2.5.4.jar:2.5.4] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.4.jar:2.5.4] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.4.jar:2.5.4] 在 com.batch.CsvTomysqlApplication.main(CsvToMysqlApplication.java:10) ~[classes/:na]
[2m2021-08-28 10:24:36.741[0;39m [32m INFO[0;39m [35m10388[0;39m [2m---[0;39m [2m[主][0;39m [36mo.s.batch.core.step.AbstractStep [0;39m [2m:[0;39m 步骤:[step1] 在 72ms 内执行 [2m2021-08-28 10:24:36.768[0;39m [32m 信息[0;39m [35m10388[0;39m [2m---[0;39m [2m[ 主][0;39m [36mo.s.b.c.l.support.SimpleJobLauncher [0;39m [2m:[0;39m Job: [FlowJob: [name=USER-IMPORT-JOB]] 完成 以下参数:[run.id=4] 和以下状态:[FAILED] 在 134 毫秒内'
块引用
【问题讨论】:
您没有包含 NPE 所在的特定类,但很可能问题在于它不是 Spring bean。避免使用@Autowired
字段;通常将它们替换为构造函数或(在@Bean
方法的情况下)参数。
谢谢你的回复,但我没有得到你的正确答案,你能解释一下吗,非常感谢
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。
【参考方案1】:
请将 LineMapper 方法设为 public 并添加 @Bean 注解。还要在 JdbcBatchItemWriter 方法上方添加@Bean 注解。
@Bean
public LineMapper<User> getlineMapper()
DefaultLineMapper<User> lineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
lineTokenizer.setNames(new String[] "Emp ID", "Name Prefix", "First Name", "Last Name" );
lineTokenizer.setIncludedFields(new int[] 0, 1, 2, 4 );
BeanWrapperFieldSetMapper<User> fieldSetMapper = new BeanWrapperFieldSetMapper<>();
fieldSetMapper.setTargetType(User.class);
lineMapper.setLineTokenizer(lineTokenizer);
lineMapper.setFieldSetMapper(fieldSetMapper);
return lineMapper;
@Bean
public JdbcBatchItemWriter<User> writer()
JdbcBatchItemWriter<User> writer = new JdbcBatchItemWriter<>();
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<User>());
writer.setSql(
"insert into user(userId,namePrefix,firstName,lastName) values (:userId, :namePrefix, :firstName, :lastName)");
writer.setDataSource(this.dataSource);
return writer;
【讨论】:
【参考方案2】:为简单起见,使用一个单独的临时用户类作为 UserInput 来读取数据和一个表示表 e.i *User 实体的实体类
用户输入
public class UserInput
private String userId;
private String namePrefix;
private String firstName;
private String lastName;
用户
@Entity
@Table(name = "USER")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String userId;
private String namePrefix;
private String firstName;
private String lastName;
用户数据处理器
public class UserDataProcessor implements ItemProcessor<UserInput, User>
@Override
public User process(UserInput userInput) throws Exception
User user = new User();
user.setUserId(userInput.getUserId());
user.setNamePrefix(userInput.getNamePrefix());
user.setFirstName(userInput.getFirstName());
user.setLastName(userInput.getLastName());
System.out.println(user);
return user;
批量配置
@Configuration
@EnableBatchProcessing
public class BatchConfig
private final String[] FIELD_NAMES = new String[] "userId", "namePrefix", "firstName", "lastName" ;
@Autowired
private DataSource dataSource;
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public FlatFileItemReader<UserInput> reader()
return new FlatFileItemReaderBuilder<UserInput>().name("personItemReader")
.resource(new ClassPathResource("user-data.csv")).delimited().names(FIELD_NAMES)
.fieldSetMapper(new BeanWrapperFieldSetMapper<UserInput>()
setTargetType(UserInput.class);
).build();
@Bean
public UserDataProcessor processor()
return new UserDataProcessor();
@Bean
public JdbcBatchItemWriter<User> writer()
return new JdbcBatchItemWriterBuilder<User>()
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
.sql("INSERT INTO user (userId,namePrefix,firstName,lastName) "
+ "VALUES (:userId,:namePrefix,:firstName,:lastName)")
.dataSource(dataSource).build();
@Bean
public Job importUserJob(JobCompletionNotificationListener listener, Step step1)
return jobBuilderFactory.get("importUserJob").incrementer(new RunIdIncrementer()).listener(listener).flow(step1)
.end().build();
@Bean
public Step step1(JdbcBatchItemWriter<User> writer)
return stepBuilderFactory.get("step1").<UserInput, User>chunk(10).reader(reader()).processor(processor())
.writer(writer).build();
【讨论】:
以上是关于使用spring批处理和csv文件格式获取空指针异常向db发送数据的主要内容,如果未能解决你的问题,请参考以下文章
java中String browser = request.getHeader(&quot;user-agent&quot;)报空指针异怎么解决
spring利用SecurityContextHolder获取用户信息的session 出现空指针异常,应该是我没设置,但不知道哪里设
更改 csv 文件中的日期时间格式 - 批处理脚本/Powershell [关闭]