0

The file "icon.png" is in the same folder as class. But if i just use (new Image("icon.png")) then it says java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found. But now after adding getClass().getResource... i'm getting this error. Here is my code:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;

public class Main extends Application {
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("NewFile.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setResizable(false);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.getIcons().add(new Image(getClass().getResource("icon.png").toExternalForm()));
            primaryStage.show();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

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

And here is the error:

java.lang.NullPointerException
    at application.Main.start(Main.java:19)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
    at java.base/java.lang.Thread.run(Unknown Source)

Please help me to fix it. Thanks in advance!

lospejos
  • 1,888
  • 3
  • 17
  • 31

1 Answers1

0

NullPointerException error is caused due to location of your image is not in the proper position your refer to something which not exist you can edit your post to show us your project construction
it should be somthing like that to work !

enter image description here

Mazen Embaby
  • 969
  • 8
  • 13