位置未设定

时间:2018-05-20 19:12:15

标签: java javafx

我在尝试运行代码时遇到此错误,它是一个简单的javafx代码来加载fxml文件,我尝试了我在这里找到的解决方案,但没有什么对我有用。对不起,如果格式不是很好,这是我在这里的第一篇文章,对不起,如果我正在屠杀这门语言,英语不是我的第一语言。提前谢谢!

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

1 个答案:

答案 0 :(得分:0)

当输入为FXMLLoader时,

Location is not set会抛出“null”。

在您的情况下,它发生是因为您提供了错误的XML绝对路径。 XML的绝对路径应为/projeto/resources/RootLayout.fxml

projeto

修复绝对路径:

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

或使用相对路径:

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

(注意缺少前导/