0

I have problem with my code. Each query for the OpenWeatherMap service should save variable oftemperature and date to the base.Unfortunately, I have a problem with a nullpointer. I found information on the Internet that you can not perform in Quartz jobs operations on a database, but i dont have any idea how to change this code.

Its my quartz job class

@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();
        }
    }
}

Null Pointer is in line, where is comment. Can you give me advice how to solve this problem?

pawel033
  • 11
  • 1
  • 4

0 Answers0