'Unexpected error occurred in scheduled task
I tried to run a scheduled task in my Spring-Boot application, using the default applicationConfig
- I only added the annotation @EnableScheduling
and configured the class with the method that is to be scheduled, per the following code sample:
@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;
}
}
However, when the task is called, the method callService
returns the following error:
java.lang.NullPointerException: null
This, quite obviously, makes me sad.
Solution 1:[1]
thanks for the comments I solved this added next code to init method
public Task(){
client = new HttpClient();
client.getParams().setParameter("http.useragent", "Bacon/1.0");
}
thanks to @edgarNgwenya
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jaimeRambo |