0

I am writing Weather Application to forecast weather for 2 cities. I use JavaFx, maven, SceneBuilder. I get an error while changing first city. The second city works good.

I get this error:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
    ... 47 more
Caused by: net.aksingh.owmjapis.api.APIException: API call gave error: 400 - Bad Request
    at owm.japis@2.5.3.0/net.aksingh.owmjapis.core.OWM.hourlyWeatherForecastByCityName(OWM.kt:331)
    at WeatherApp/com.dariuszkrygier.model.WeatherForecastFetcher.getWeatherForecast(WeatherForecastFetcher.java:16)
    at WeatherApp/com.dariuszkrygier.controller.MainWindowController.showCurrentWeather(MainWindowController.java:285)
    at WeatherApp/com.dariuszkrygier.controller.MainWindowController.changeFirstCityLocationButtonAction(MainWindowController.java:202)
    ... 58 more

Process finished with exit code 130

Problems caused by are bolded:



 public WeatherForecastFetcher(String apiKey) {
        owm = new OWM(apiKey);
        owm.setUnit(OWM.Unit.METRIC);
    }

    public Weather getWeatherForecast(String cityNameWithCountryCode) throws APIException {
**        HourlyWeatherForecast hourlyWeatherForecast = owm.hourlyWeatherForecastByCityName(cityNameWithCountryCode);**
        if (hourlyWeatherForecast.hasRespCode() && hourlyWeatherForecast.getRespCode().equals("200")) {
            return new Weather(hourlyWeatherForecast);
        }
        return null;
    }
}





private void showCurrentWeather(TextField locationField, Label locationName, Label locationDate,
                                    ImageView locationImage, Label locationTemp, Label locationWeather
                                    ) throws APIException {

        **var weather = weatherForecastFetcher.getWeatherForecast(locationField.getText());**
        int currentTimeIndex = 0;

        String cityNameWithCountryCode = weather.getCityName() + ", " + weather .getCountryCode();
        String dateWithDay =
                weather.getDayOfTheWeek(currentTimeIndex) + ", " + weather.getDateWithoutTime(currentTimeIndex);
        Image image = new Image(weather .getIconLink(currentTimeIndex));
        String temp = weather .getTemp(currentTimeIndex);
        String description = weather .getDescription(currentTimeIndex);

        locationName.setText(cityNameWithCountryCode);
        locationDate.setText(dateWithDay);
        locationImage.setImage(image);
        locationTemp.setText(temp);
        locationWeather.setText(description);

    }





@FXML
    void changeFirstCityLocationButtonAction() throws APIException {
        if (firstFieldIsValid()) {
           ** showCurrentWeather(firstCityPicker, firstCity, firstCityFirstDayDate, firstCityFirstDayImage,
                    firstCityFirstDayTemp, firstCityFirstDayDescription );**
            //day2
            showNextDays(firstCityPicker, 8, firstCitySecondDayDate, firstCitySecondDayImage,
                    firstCitySecondDayTemp, firstCitySecondDayDescription);
            //day3
            showNextDays(firstCityPicker, 16, firstCityThirdDayDate, firstCityThirdDayImage,
                    firstCityThirdDayTemp, firstCityThirdDayDescription);
            //day4
            showNextDays(firstCityPicker, 24, firstCityFourthDayDate, firstCityFourthDayImage,
                    firstCityFourthDayTemp, firstCityFourthDayDescription);
            //day5
            showNextDays(firstCityPicker, 32, firstCityFifthDayDate, firstCityFifthDayImage,
                    firstCityFifthDayTemp, firstCityFifthDayDescription);
        }
    }


The path to github is:

https://github.com/dariuszkrygier/WeatherApp.git

Please help me.

dar1o
  • 1
  • 1
  • 1
    You are getting a [HTTP `400` error](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400#). Provide the server with the data it is able to understand (currently you are not doing that). What the server requires is API specific and will be documented in the server API documentation, to which you must conform. If still in doubt, request help directly from the maintainers of the `OWM` API that you are using. – jewelsea Jul 26 '23 at 22:13
  • 1
    Learn how to [read a stack trace](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). The `InvocationTargetException` is a symptom of the error and not the ultimate cause, which is indicated in the trace by `Caused by: net.aksingh.owmjapis.api.APIException: API call gave error: 400 - Bad Request`. – jewelsea Jul 26 '23 at 22:16
  • 1
    Instead of *"JavaFX,Maven: Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException"*, your question should actually be, *"What is the correct input to use for the openweathermap API call: `owm.hourlyWeatherForecastByCityName(cityNameWithCountryCode)`?"* and in the question, provide the name of the city you want a weather forecast for and the actual value you are providing to the method call which is failing (which you can obtain through a debugger or log statement), also check the API authentication credentials used. – jewelsea Jul 26 '23 at 22:25

0 Answers0