0

I am trying to run multi step spring batch job with tasklet.Inside concreat implementation of tasklet trying to use other services with @Autowired . But not working and facing null pointer exception :

java.lang.NullPointerException: null at com.rml.api.poc.MyTaskTwo.getStackStatus(MyTaskTwo.java:52) at com.rml.api.poc.MyTaskTwo.execute(MyTaskTwo.java:41) at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330) at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)

Example code is given bellow :

  @Configuration
    @EnableBatchProcessing
    public class BatchConfig {
        @Autowired
        private JobBuilderFactory jobs;

        @Autowired
        private StepBuilderFactory steps;



        @Bean
        public Step stepOne() {
            return steps.get("stepOne")
                    .tasklet(new MyTaskOne())
                    .build();
        }

        @Bean
        public Step stepTwo() {
            return steps.get("stepTwo")
                    .tasklet(new MyTaskTwo())
                    .build();
        }

        @Bean
        public Step stepThree() {
            return steps.get("stepThree")
                    .tasklet(new MyTaskThree())
                    .build();
        }

        @Bean
        public Job helloJob() {

            return jobs.get("createProject")
                    .start(stepOne())
                    .next(stepTwo())
                    .next(stepThree())
                    .build();
        }

    }

Tasklet classes :

public class MyTaskOne implements Tasklet {

            @Autowired
            HelloService helloService;

            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

                //custom code 
                return RepeatStatus.FINISHED;
            }
        }


public class MyTaskTwo implements Tasklet {

        @Autowired
        HelloService helloService;

        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

            //custom code 
            return RepeatStatus.FINISHED;
        }
    }


public class MyTaskThree implements Tasklet {

        @Autowired
        HelloService helloService;

        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

            //custom code 
            return RepeatStatus.FINISHED;
        }
    }

What i am missing here ?

Asraful
  • 1,241
  • 18
  • 31
  • The Tasklets must be returned from `@Bean` methods, or scanned and injected. If you create them using new, and don't return them from `@Bean` methods, then they're not Spring beans, and Spring will thus not inject anything in it. – JB Nizet Jan 11 '20 at 15:23
  • Instead of using `@Autowired` in `Tasklet` implementations you could change it to use a service which would be passed in a constructor. Otherwise @jb-nizet observation should be considered – Ivar Jan 11 '20 at 15:33
  • Issue is fixed long ago , i will post the solution – Asraful Aug 27 '20 at 17:11

0 Answers0