0

I'm trying to create a Scene class from a fxml file in the path src/interfaces/panels/, and the file is called StartUp.fxml, but I get this error:

java.lang.NullPointerException: Location is required.
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
        at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
        at tienda.TIENDA.start(TIENDA.java:30)
public void start(Stage stage) throws Exception {
        window=stage;
        String path = new File("src/interfaces/panels/StartUp.fxml").getAbsolutePath();
        System.out.println(path);//I added this just in case
        Parent root = FXMLLoader.load(getClass().getResource("src/interfaces/panels/StartUp.fxml"));//this is the problem
        su=new Scene(root);
        window.setScene(su);
        window.show();
    }

EDIT: This was the solution TIENDA is the class, and the path is relative to the class

public void start(Stage stage) throws Exception {
        window=stage;
        var resource = TIENDA.class.getResource("../src/interfaces/panels/StartUp.fxml");//HERE
        System.out.println(resource);
        Parent root = FXMLLoader.load(resource);
        su=new Scene(root);
        window.setScene(su);
        window.show();
    }
  • You pass either a `null` or a malformed object to `FXML`. Likely the resource path cannot be resolved. Create a variable via `var resource = getClass().getResource("src/interfaces/panels/StartUp.fxml")`, I presume if you debug this then and check the variables you will find this to be true. – Koenigsberg Feb 26 '21 at 00:53
  • I printed the resource value, and it is null, so now I understand why the error happens, but then, why does getResource() return null? – eduardo guevara lozano Feb 26 '21 at 01:16
  • Otherwise check paths and the actual location etc. – Koenigsberg Feb 26 '21 at 01:18
  • Thanks, the problem was that I didn't know I had to use getResource from the main class and put a relative path from there. – eduardo guevara lozano Feb 26 '21 at 01:51

0 Answers0