Spring boot 计划的任务中出现意外错误
我尝试在我的Spring Boot应用程序中使用默认的Spring boot 计划的任务中出现意外错误,spring-boot,scheduler,Spring Boot,Scheduler,我尝试在我的Spring Boot应用程序中使用默认的applicationConfig,运行计划任务-我只添加了注释@EnableScheduling,并根据以下代码示例使用要计划的方法配置了类: @Component public class Task { @Scheduled(cron ="*/10 * * * * *") public void init() { System.out.println("QuartzConfig initi
applicationConfig
,运行计划任务-我只添加了注释@EnableScheduling
,并根据以下代码示例使用要计划的方法配置了类:
@Component
public class Task {
@Scheduled(cron ="*/10 * * * * *")
public void init() {
System.out.println("QuartzConfig initialized.");
System.out.println(callService());
}
private String callService() {
String urlService = "https://www.google.com";
GetMethod method = new GetMethod(urlService );
String response = "";
try {
client.executeMethod(method);
response = method.getResponseBodyAsString();
} catch (IOException e) {
e.printStackTrace();
} finally {
method.releaseConnection();
}
return response;
}
}
但是,调用任务时,方法callService
返回以下错误:
java.lang.NullPointerException: null
很明显,这让我很难过。谢谢你的评论,我解决了这个问题,为init方法添加了下一个代码
public Task(){
client = new HttpClient();
client.getParams().setParameter("http.useragent", "Bacon/1.0");
}
感谢@edgarNgwenya哪一行是例外?最明显的罪魁祸首是client==null。客户机设置在哪里?是的@edgarNgwenya现在测试添加了客户机的init方法,运行良好,感谢您的观点。