First time using Spring Batch, seems pretty cool. Just one thing kind of confused me.
I setup a quick test to parse a tab-delimitted file and I used the Generics to supply the classname and thought by doing so I would not need to supply a FieldSetMapper.
However as I tested the code I kept getting NullPointerException and had trouble figuring out why, until I tried supplying a FieldSetMapper and then it worked.
Here is the code:
So this code works fine, however if you comment out the "mapper.setFieldSetMapper(wrapper);" the operation throws a NullPointerException which I thought was odd because I thought by instantiating the FlatFileItemReader with <AddressVO> that it would not need the FieldSetMapper, that would be a redundant step, no?Code:public void handleFile () throws Exception { File file = new File("D:/input/input.in"); DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer(); tokenizer.setDelimiter(DelimitedLineTokenizer.DELIMITER_TAB); tokenizer.setNames(new String[] { "address1", "address2", "address3", "city", "state" }); BeanWrapperFieldSetMapper<AddressVO> wrapper = new BeanWrapperFieldSetMapper(); wrapper.setTargetType(AddressVO.class); DefaultLineMapper<AddressVO> mapper = new DefaultLineMapper<AddressVO>(); mapper.setLineTokenizer(tokenizer); mapper.setFieldSetMapper(wrapper); FlatFileItemReader<AddressVO> reader = new FlatFileItemReader<AddressVO>(); reader.setLineMapper(mapper); reader.setResource(new FileSystemResource(file)); reader.open(new ExecutionContext()); AddressVO vo = null; while ((vo = reader.read()) != null) { System.out.println("Read next item: "+vo.getAddress1()+", "+vo.getCity()+", "+vo.getState()); } reader.close(); }


Reply With Quote