Ich verwende Spring-Planung, um eine Aufgabe zu planen, die jede Stunde DB-Vorgang ausführt. Dies löst ein NullPointerException
jedes Mal, wenn ich versuche, eine Funktion aufzurufen, die DB-spezifische Vorgänge auslöst.Verwenden von Spring Scheduler zum Zurückgeben von Zeilen aus DB wirft Nullzeiger Ausnahme
Konfigurationsdatei:
@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();
}
}
Armaturenbrett Controller:
@RestController
public class DashboardController {
@Autowired
private InlineService service;
@RequestMapping(value="/rtd/getAllServers", method=RequestMethod.GET)
public ResponseEntity<List<ServerMonitor>> getAllServers(){
List<ServerMonitor> ilsList=new ArrayList<ServerMonitor>();
ResponseEntity<List<ServerMonitor>> response=null;
try{
ilsList=service.getAllServers();
response=new ResponseEntity<List<ServerMonitor>>(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<IlsStats> ilsStats =new ArrayList<IlsStats>();
List<IlsStatsHistory> ilsStatsHisList=new ArrayList<IlsStatsHistory>();
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<ServerMonitor> ilsList=service.getAllServers();
}catch(Exception e){
e.printStackTrace();
}
}
Hier ilsStats=service.getAllInlineStats();
wirft NullPointerException
.
Auch wenn der Rest Webservice, der darauf erstellt wird, funktioniert gut.
Stack Trace:
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)
Das gleiche Stack-Trace kommt jetzt alle 1 Sek bis.
Mögliche Duplikate von [Was ist eine NullPointerException, und wie behebe ich es?] (Http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -it) – Jens
Und Sie fügen nicht die Stack-Trace noch die tatsächlichen Bohnen, die es betrifft ... Bitte lesen Sie http://StackOverflow.com/help/how-to-ask und verbessern Sie Ihre Frage nach dem Lesen. –
@M.deinum Mein schlechtes. Dies ist das erste Mal, dass ich auf dieser Plattform bin. Ich habe den Stack-Trace und den Controller-Code hinzugefügt. –