我的代码有问题。 对OpenWeatherMap服务的每个查询都应将温度和日期变量保存到基准中。不幸的是,我的null指针有问题。我在Internet上找到了您无法在数据库的Quartz作业中执行的信息,但是我不知道如何更改此代码。
这是我的石英作业班
@Component
@NoArgsConstructor
public class QuartzJob implements Job {
private final String prefix = "https://api.openweathermap.org/data/2.5/weather?q=";
private final String key = "&units=metric&appid=b25d9b1a1e64e5e91ed9d14b74c1ca01";
@Autowired
private WeatherModelRepository weatherModelRepository;
@Autowired
private CityModelRepository cityModelRepository;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
String cityName = String.valueOf(jobExecutionContext.getJobDetail().getKey()).substring(8);
String myUrl = prefix+cityName+key;
Weather weather = new Weather();
try{
URL url1 = new URL(myUrl);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonRoot = objectMapper.readTree(url1);
JsonNode jsonMain = jsonRoot.path("main");
JsonNode jsonTemperature = jsonMain.path("temp");
Date date = new Date();
weather.setTemperature(jsonTemperature.asText());
weather.setTimestamp(new Timestamp(date.getTime()));
//weather.setCityModel(cityModelRepository.findByCityName(cityName));
System.out.println(cityName);
//weatherModelRepository.save(weather);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
空指针在行中,注释在哪里。您能给我建议如何解决这个问题吗?