Issue
I'm trying to figure out Spring Batch. I try to read data from file but got exception. This is my POJO:
public class VehicleData {
private Long vehicleId;
private Date timestamp;
private Double latitude;
private Double longitude;
public VehicleData(Long vehicleId, Date timestamp, Double latitude, Double longitude) {
this.vehicleId = vehicleId;
this.timestamp = timestamp;
this.latitude = latitude;
this.longitude = longitude;
}
//getters and setters
}
First line of the file:
1,2012-02-02 14:37:30,112.29369,43.92272
Here is the reader Bean:
@Bean
public FlatFileItemReader<VehicleData> reader() {
FlatFileItemReader<VehicleData> reader = new FlatFileItemReader<>();
reader.setResource(new ClassPathResource("data/1.txt"));
reader.setLineMapper(new DefaultLineMapper<VehicleData>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[]{"vehicleId", "timestamp", "latitude", "longitude"});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<VehicleData>() {{
setTargetType(VehicleData.class);
setCustomEditors(Collections.singletonMap(Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), false)));
}});
}});
return reader;
}
Error is as follows:
org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 1 in resource=[class path resource [data/1.txt]], input=[1,2012-02-02 14:37:30,112.29369,43.92272]
at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:184) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:157) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
...
Caused by: java.lang.reflect.UndeclaredThrowableException: null
at org.springframework.util.ReflectionUtils.handleReflectionException(ReflectionUtils.java:285) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.getBean(BeanWrapperFieldSetMapper.java:236) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.mapFieldSet(BeanWrapperFieldSetMapper.java:185) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:43) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:180) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
... 51 common frames omitted
Caused by: java.lang.InstantiationException: com.gom.tom.simulator.VehicleData
at java.lang.Class.newInstance(Class.java:427) ~[na:1.8.0_102]
at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.getBean(BeanWrapperFieldSetMapper.java:233) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
... 54 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.gom.tom.simulator.VehicleData.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_102]
at java.lang.Class.newInstance(Class.java:412) ~[na:1.8.0_102]
... 55 common frames omitted
I was suspicious about Date and wrote a custom editor for that purpose. What may be the reason for that?
Solution
NoSuchMethodException: com.gom.tom.simulator.VehicleData.<init>()
means that you are missing the default constructor in your POJO.
public class VehicleData {
// ....
public VehicleData() {
}
// ....
}
Answered By - tima
Answer Checked By - Marie Seifert (JavaFixing Admin)