JavaFX中的main方法抛出异常

时间:2018-07-26 21:09:53

标签: javafx

使用javafx执行我的主要功能时遇到了一些问题。如果我没记错的话,似乎在加载fxml文件时出现了问题,但是我似乎找不到解决方法。

异常消息:

Exception in Application start method java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)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(Unknown Source)
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 Gestion.view.MainClass.initialisationContenu(MainClass.java:57)
at Gestion.view.MainClass.start(MainClass.java:28)
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 Gestion.view.MainClass

主要功能:

package Gestion.view;

import java.io.IOException;
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;

public class MainClass extends Application {
    private Stage stagePrincipal;
    private BorderPane conteneurPrincipal;

    @Override
    public void start(Stage primaryStage) {
        stagePrincipal = primaryStage;

        stagePrincipal.setTitle("Application de gestion du centre socio-médical OCP");


        initialisationConteneurPrincipal();
        initialisationContenu();
    }

    private void initialisationConteneurPrincipal() {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainClass.class.getResource("Welcome.fxml"));
        try {

            conteneurPrincipal = (BorderPane) loader.load();
            Scene scene = new Scene(conteneurPrincipal);
            stagePrincipal.setScene(scene);
            stagePrincipal.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initialisationContenu() {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass()
                .getClassLoader()
                .getResource("Acceuil1.fxml")
        );
        try {
            AnchorPane conteneurPersonne = (AnchorPane) loader.load();
            conteneurPrincipal.setCenter(conteneurPersonne);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

    public Stage getStage() {
        return null;
    }
}

我已经搜索了其他类似的问题,但是给出的答案对我不起作用。

1 个答案:

答案 0 :(得分:0)

问题是FXMLLoader无法从FXML文档加载对象层次结构。

通过更新网址进行修复:

loader.setLocation(MainClass.class.getResource("/fxml/Welcome.fxml"));

.getResource("/fxml/Acceuil1.fxml")

并将 Welcome.fxml Acceuil1.fxml 移到 src / main / resources / fxml 目录。