Using Spring Scheduler to return rows from DB throws Null pointer Exception
<p>I am using Spring scheduling to schedule a task that performs some DB operation every hour. This throws a <code>NullPointerException</code> every time I try to call a function that triggers DB specific operations.</p>

<p><strong>Configuration File :</strong></p>

<pre><code>@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.bt.rtddashboard")
/*@EnableJpaRepositories(basePackages = {
        "com.bt.rtddashboard"
})*/
@EnableScheduling
@PropertySource("classpath:jdbc.properties")
public class RTDDashboardConfiguration extends WebMvcConfigurerAdapter {


    @Resource
    private Environment env;

    @Bean(name = "dataSource", destroyMethod="close")
    public DataSource getDataSource() {
        //DB code
    }

    private Properties getHibernateProperties() {
            //Hibernate code
    }

    @Bean(name = "sessionFactory")
    @Scope("singleton")
    public LocalSessionFactoryBean getSessionFactory() {

    }

    @Autowired
    @Bean(name = "transactionManager")
    public HibernateTransactionManager getHibernateTransactionManager(SessionFactory factory) {
    ;
    }

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/").setCachePeriod(31556926);

    }

    @Scheduled(fixedRate=1000)
    public void work() {
        // task execution logic
        System.out.println("Scheduled Task");
        DashboardController controller=new DashboardController();
        controller.performTask();
    }

}
</code></pre>

<p><strong>Dashboard Controller :</strong></p>

<pre><code>@RestController
public class DashboardController {

    @Autowired
    private InlineService service;

    @RequestMapping(value="/rtd/getAllServers", method=RequestMethod.GET)
    public ResponseEntity&lt;List&lt;ServerMonitor&gt;&gt; getAllServers(){
        List&lt;ServerMonitor&gt; ilsList=new ArrayList&lt;ServerMonitor&gt;();
        ResponseEntity&lt;List&lt;ServerMonitor&gt;&gt; response=null;

        try{
            ilsList=service.getAllServers();
            response=new ResponseEntity&lt;List&lt;ServerMonitor&gt;&gt;(ilsList,HttpStatus.OK);
            System.out.println("Scheduled Time");

        }catch(Exception e){

        }
        return response;
    }

public  void  performTask(){
        System.out.println("Scheduled Task Starts in Contrroller");
        List&lt;IlsStats&gt; ilsStats =new ArrayList&lt;IlsStats&gt;();
        List&lt;IlsStatsHistory&gt; ilsStatsHisList=new ArrayList&lt;IlsStatsHistory&gt;();
        try{
            //get All the ILS Stats
            ilsStats=service.getAllInlineStats();
            System.out.println("No of rows to Copy : " + ilsStats.size());
            ilsStatsHisList=formILSHistoryList(ilsStats);

            int flag=service.insertInIlsStatsHistory(ilsStatsHisList);

        //List&lt;ServerMonitor&gt; ilsList=service.getAllServers();

        }catch(Exception e){
            e.printStackTrace();
        }
    }
</code></pre>

<p>Here,  <code>ilsStats=service.getAllInlineStats();</code> throws <code>NullPointerException</code>.</p>

<p>Even though the rest webservice created on top of it is working fine.</p>

<p>Stack Trace :</p>

<pre><code>Scheduled Task
Scheduled Task Starts in Contrroller
java.lang.NullPointerException
    at com.bt.rtddashboard.controller.DashboardController.performTask(DashboardController.java:226)
    at com.bt.rtddashboard.configuration.RTDDashboardConfiguration.work(RTDDashboardConfiguration.java:137)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
</code></pre>

<p>The same stack trace comes up every 1 sec now.</p>