Announcement Announcement Module
Collapse
No announcement yet.
NullPointerException if I dont supply FieldSetMapper Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NullPointerException if I dont supply FieldSetMapper

    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:

    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();
    	}
    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?
    Last edited by trant; May 13th, 2011, 11:54 AM.

  • #2
    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?
    the type parameter of the FlatFileItemReader doesn't tell Spring Batch how to create an object from a FieldSet. So the FieldSetMapper is a mandatory dependency.

    Comment

    Working...
    X