"Location is not set" Javafx

Asked

Viewed 955 times

0

I’ve been studying javafx recently and got caught up in writing a very simple code to open a file. fxml, follows the code and error respectively, searched the internet and already tried the solution proposed in American stackoverflow, and even then the problem persists, anyone know how to fix it? Thank you!

Note. 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. javafx.fxml.Fxmlloader.loadImpl(Fxmlloader.java:2434) 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 project.Mainapp

1 answer

1


To load a . fxml make sure to inform the path of this file relative to the folder src of the project. The example below considers that inside the folder src has the following subfolders containing the file . fxml: 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’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.