title: JpaPagingItemReader failing to read data and move to next step
body: <p>I have a Spring batch process that reads data from a SQL Server and writes to a file. The query I am using returns 350000 rows and takes about 2 minutes to run on SQL Server studio - so the query is tuned for best performance. </p>
  
  <p>Below is the JobConfiguration class:</p>
  
  <pre><code>@Configuration
  @Slf4j
  public class JobConfiguration {
      private final JobBuilderFactory jobBuilderFactory;
      private final StepBuilderFactory stepBuilderFactory;
      private final FileProperties fileProperties;
      private JobProperties jobProperties;
      private FileUtility fileUtility;
      private CompensationDetailStatusRepository compensationDetailStatusRepository;
      private CompensationDetailsRepository compensationDetailsRepository;
  
      @Autowired
      public JobConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory,
                              JobProperties jobProperties, FileProperties fileProperties
      ) {
          this.jobBuilderFactory = jobBuilderFactory;
          this.stepBuilderFactory = stepBuilderFactory;
          this.jobProperties = jobProperties;
          this.fileProperties = fileProperties;
      }
  
      @Bean
      public Job processExtractJob(ExtractProcessListener listener,
                                          Step processExtract,
                                          Step moveFiles
                                          ) {
          return jobBuilderFactory.get("processExtractJob")
                  .incrementer(new RunIdIncrementer())
                  .listener(listener)
                  .start(processExtract)
                  .on("COMPLETED")
                  .to(moveFiles)
                  .end()
                  .build();
      }
  
      @Bean
      public Step processExtract(ItemReader&lt;Person&gt; PersonReader,
                                        ExtractProcessor extractProcessor,
                                        ExtractWriter extractWriter
      ) {
          return stepBuilderFactory.get("processExtract")
                  .&lt;Person, ExtractVO&gt;chunk(50)
                  .faultTolerant()
                  .retryLimit(1)
                  .retry(IOException.class)
                  .retry(UncheckedIOException.class)
                  .reader(PersonReader)
                  .processor(extractProcessor)
                  .writer(extractWriter)
                  .build();
      }
  
      @Bean
      public ExtractWriter extractWriter() {
          return new ExtractWriter();
      }
  
      @Bean
      public ExtractProcessor extractProcessor() {
          return new ExtractProcessor();
      }
  
      @Bean
      public Step moveFiles() {
          return stepBuilderFactory.get("moveFiles")
                  .tasklet((contribution, chunkContext) -&gt; {
                      fileUtility.moveFiles(String.format("%s*", fileProperties.getFileNamePrefix()),
                              fileProperties.getFileStagingPath(), fileProperties.getFileTargetPath());
                      return RepeatStatus.FINISHED;
                  }).build();
      }
  
      @Bean
      @StepScope
      public ItemReader&lt;Person&gt; PersonReader(EntityManagerFactory entityManagerFactory) {
          String query = "Select new com.example.Person (DTL.personId,DTL.description) FROM DETAIL DTL;";
          JpaPagingItemReader&lt;Person&gt; reader = new JpaPagingItemReader&lt;&gt;();
          reader.setQueryString(query);
          reader.setTransacted(false);
          reader.setPageSize(jobProperties.getReaderPageSize());
          reader.setEntityManagerFactory(entityManagerFactory);
          reader.setPageSize(jobProperties.getReaderPageSize());
          return reader;
      }
  
      @Bean
      public ExtractProcessListener jobExecutionListener() {
          return new ExtractProcessListener();
      }
  }
  </code></pre>
  
  <p>Below is the java object (Person.java) for the database resultset:</p>
  
  <pre><code>@ToString
  @AllArgsConstructor
  @NoArgsConstructor
  @Data
  public class PersonCompensation {
      private String personId;
      private String description;
  }
  </code></pre>
  
  <p>When I run the batch process, I get the following exception:</p>
  
  <pre><code>2020-04-15 21:34:59.172 ERROR 11748 --- [rTaskExecutor-1] o.s.batch.core.step.AbstractStep         : Encountered an error executing step processExtract in job processExtractJob
  org.springframework.batch.core.step.skip.NonSkippableReadException: Non-skippable exception during read
      at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:105) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
      at org.springframework.batch...
  Caused by: java.lang.NullPointerException: null
      at org.springframework.batch.item.database.JpaPagingItemReader.doReadPage(JpaPagingItemReader.java:196) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
      at org.springframework.batch.item.database.AbstractPagingItemReader.doRead(AbstractPagingItemReader.java:108) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
      at org.springframework.batch.item.support...
      ... 27 common frames omitted
  2020-04-15 21:34:59.946  WARN 11748 --- [rTaskExecutor-1] o.s.b.f.support.DisposableBeanAdapter    : Invocation of destroy method 'close' failed on bean with name 'scopedTarget.personCompensationReader': org.springframework.batch.item.ItemStreamException: Error while closing item reader
  2020-04-15 21:35:00.277 ERROR 11748 --- [rTaskExecutor-1] o.s.batch.core.job.AbstractJob           : Encountered fatal error executing job
  org.springframework.batch.core.JobExecutionException: Flow execution ended unexpectedly
      at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:140) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
      at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
      at org.springframework.batch.core...
  Caused by: org.springframework.batch.core.job.flow.FlowExecutionException: Next state not found in flow=processExtractJob for state=processExtractJob.processExtract with exit status=FAILED
      at org.springframework.batch.core.job.flow.support.SimpleFlow.nextState(SimpleFlow.java:230) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
      at org.springframework.batch.core...
  Exit Code: FAILED
  Exit Desc: org.springframework.batch.core.step.skip.NonSkippableReadException: Non-skippable exception during read
      at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:105)
      at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:116)
      at org.springframework.batch...
  Caused by: java.lang.NullPointerException
      at org.springframework.batch.item.database.JpaPagingItemReader.doReadPage(JpaPagingItemReader.java:196)
      at org.springframework.batch.item.database.AbstractPagingItemReader.doRead(AbstractPagingItemReader.java:108)
      at org.springframework.batch.item...
      ... 27 more
  </code></pre>
  
  <p>I have three questions:</p>
  
  <ol>
  <li>Does the spring batch process query and fetch all the 350000 rows at once and give the records to processor in chunks of 50 (chunk size) ?</li>
  <li>I have this line in the code <code>JpaPagingItemReader&lt;Person&gt; reader = new JpaPagingItemReader&lt;&gt;();</code>. Should the Person class be a Entity object for this to work or any will a POJO work fine?</li>
  <li>What could be causing the above exception?</li>
  </ol>