Error trying to change JavaFX application icon

Hello. I am developing an application in JavaFX and I am trying to change the icon of my application as per the code below:

public class Login extends Application 

{

Image applicationIcon = new Image("/src/Icons/message.png");

@Override
public void start(Stage primaryStage) 

{

try 

    {
        Parent root = FXMLLoader.load(getClass().getResource("../FXML/Login.fxml"));
        Scene scene = new Scene(root);
        primaryStage.setTitle("Login");

        primaryStage.getIcons().add(new Image(Login.class.getResourceAsStream("src/Icons/message.png")));           
        primaryStage.getIcons().add(applicationIcon);
        primaryStage.setScene(scene);
        primaryStage.show();


    } 

catch (IOException e) 

    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public static void main(String[] args) {
    launch(args);
}

}

However, when running the script to change the application icon, the following error occurs:

Exception in application constructor java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source) at java.lang.reflect.Method.invoke (Unknown Source) at with.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) at with.sun.javafx.application.LauncherImpl.launchApplication (LauncherImpl.java:328) at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source) at java.lang.reflect.Method.invoke (Unknown Source) at sun.launcher.LauncherHelper F FXHelper.main (Unknown Source) Caused by: java.lang.RuntimeException: Unable to construct Application instance: class application.Login at with.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907) at with.sun.javafx.application.LauncherImpl.lambda la launchApplication$159(LauncherImpl.java:182) at java.lang.Thread.run (Unknown Source) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance (Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Unknown Source) at java.lang.reflect.Builder.newInstance (Unknown Source) at with.sun.javafx.application.LauncherImpl.lambda$launchApplication1$165 (LauncherImpl.java:819) at with.sun.javafx.application.PlatformImpl.lambda run runAndWait 1 179 (PlatformImpl.java:326) at with.sun.javafx.application.PlatformImpl.lambda nul null$177 (PlatformImpl.java:295) at java.security.AccessController.doPrivileged (Native Method) at with.sun.javafx.application.PlatformImpl.lambda run runLater$178 (PlatformImpl.java:294) at with.sun.glass.ui.InvokeLaterDispatcher Future Future.run (InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at with.sun.glass.ui.win.WinApplication.lambda nul null$152 (WinApplication.java:177) ... 1 more Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found at javafx.scenes.image.Image.validateUrl (Image.java:1118) at javafx.scenes.image.Image.(Picture.java:620) at application.Login.(Login.java:24) ... 13 more Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found at javafx.scenes.image.Image.validateUrl (Image.java:1110)... 15 more Exception running application application.Login

The files of the program I am developing are arranged as follows:

insert the description of the image here

Does anyone know why this error occurs? Thanks.

Author: Rodrigo Albuquerque, 2019-05-17

1 answers

It's been a while since I use JavaFX, but from what I'm seeing the problem is in the image path, "src/Icons/message.png '

It is looking for the src/Icons folder from the relative path of the class try to use in a similar way to the FXML file as"../ Icons / message.png '

 0
Author: Henrique Luiz, 2019-05-17 11:22:31