0

I've been getting this error when trying to run my code, it's a simple javafx code to load a fxml file, I tried the solutions that i found here but nothing is working for me. Sorry if the formatting is not very good, this is my first post here, and sorry if i'm butchering the language, english is not my first language. Thanks in advance!


package projeto;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.IOException;

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
        final FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/resources/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 {

        final FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/resources/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 projeto.MainApp.initRootLayout(MainApp.java:34)
    at projeto.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 projeto.MainApp

And here's how things are organized

Eduardo Abreu
  • 155
  • 2
  • 11

1 Answers1

0

FXMLLoader throws "Location is not set" when the input is null.

In your case it happens because you're providing a wrong absolute path to the XML. The absolute path to your XML should be /projeto/resources/RootLayout.fxml:

projeto

Either fix the absolute path:

new FXMLLoader(MainApp.class.getResource("/projeto/resources/RootLayout.fxml"));

Or use a relative path:

new FXMLLoader(MainApp.class.getResource("resources/RootLayout.fxml"));

(Notice the lack of leading /)

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • Thanks! You solved it! But now i'm getting "Caused by: java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static field rootLayout" But even if i make the rootLayout static, it still gives me errors, do you know how to fix that? – Eduardo Abreu May 20 '18 at 21:57
  • Worth noting is that the path is *not* an absolute path, you are always giving a relative path when it comes to the resources folder. An absolute path is a path that can be used anywhere, regardless of the current working directory. @rustyx – Renis1235 May 18 '21 at 09:21