代码之家  ›  专栏  ›  技术社区  ›  Eduardo Abreu

未设置位置[重复]

  •  0
  • Eduardo Abreu  · 技术社区  · 6 年前

    这个问题已经有了答案:

    我在尝试运行代码时遇到了这个错误,加载一个fxml文件是一个简单的javafx代码,我尝试了我在这里找到的解决方案,但是没有什么对我有用。抱歉,如果格式不是很好,这是我在这里的第一个帖子,如果我正在扼杀语言,英语不是我的第一语言。事先谢谢!


    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 回复  |  直到 6 年前
        1
  •  0
  •   rustyx    6 年前

    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"));
    

    (注意缺乏引导 / )