"Location is not set" JavaFX

0

I have recently studied javafx and got stuck writing a very simple code to open a .fxml file, follow the code and the error respectively, I searched the internet and tried the proposed solution on the American stackoverflow, and still the problem persists , does anyone know how to fix this? Thanks!

Obs. Forgive my formatting, it's my first time posting here.

package projeto;


public class MainApp extends Application {
    private Stage primaryStage;
    private static BorderPane rootLayout;

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

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("CineTudo");

    initRootLayout();

    showFilmeOverview();
}

public void initRootLayout(){
    try {
        //Carrega o layout root do arquivo fxml
        FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/fxml/RootLayout.fxml"));
        rootLayout = (BorderPane) loader.load();

        Scene cena = new Scene(rootLayout);
        primaryStage.setScene(cena);
        primaryStage.show();
    } catch(IOException e) {
        e.printStackTrace();

    }
    }
    public static void showFilmeOverview() {

    try {

        FXMLLoader loader = new FXMLLoader(MainApp.class.getClassLoader().getResource("/fxml/FilmeOverview.fxml"));
        AnchorPane filmeOverview = (AnchorPane) loader.load();
        rootLayout.setCenter(filmeOverview);
    }catch (IOException e){

        e.printStackTrace();
    }

    }
    public Stage getPrimaryStage() {
        return primaryStage;
    }
}
  

Exception in Application start method     java.lang.reflect.InvocationTargetException       at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke (Method.java:498)       at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs (LauncherImpl.java:389)       at com.sun.javafx.application.LauncherImpl.launchApplication (LauncherImpl.java:328)       at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke (Method.java:498)       at sun.launcher.LauncherHelper $ FXHelper.main (LauncherHelper.java:767)   Caused by: java.lang.RuntimeException: Exception in Application start method       at com.sun.javafx.application.LauncherImpl.launchApplication1 (LauncherImpl.java:917)       at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 154 (LauncherImpl.java:182)       at java.lang.Thread.run (Thread.java:748)   Caused by: java.lang.IllegalStateException: Location is not set.       at javafx.fxml.FXMLLoader.loadImpl (FXMLLoader.java:2434)       at javafx.fxml.FXMLLoader.load (FXMLLoader.java:2409)       at project.MainApp.initRootLayout (MainApp.java:34)       at project.MainApp.start (MainApp.java:25)       at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 161 (LauncherImpl.java:863)       at com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 174 (PlatformImpl.java:326)       at com.sun.javafx.application.PlatformImpl.lambda $ null $ 172 (PlatformImpl.java:295)       at java.security.AccessController.doPrivileged (Native Method)       at com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 173 (PlatformImpl.java:294)       at com.sun.glass.ui.InvokeLaterDispatcher $ Future.run (InvokeLaterDispatcher.java:95)       at com.sun.glass.ui.win.WinApplication._runLoop (Native Method)       at com.sun.glass.ui.win.WinApplication.lambda $ null $ 147 (WinApplication.java:177)       ... 1 more   Exception running application proyecto.MainApp

    
asked by anonymous 20.05.2018 / 05:33

1 answer

0

To load a .fxml make sure to enter the path of this file relative to the src folder of the project. The example below considers that within the src folder it has the following subfolders that contain the .fxml file: br/com/view .

public class MainApp extends Application {
    ...
    public void initRootLayout(){
        try {
            String caminhoPagina = "/br/com/view/FilmeOverview.fxml";
            Parent root = FXMLLoader.load(getClass().getResource(caminhoPagina));
            ...
        }
        ...
}

I hope I have helped.

    
23.05.2018 / 02:44