首页 > 解决方案 > JavaFX - 应用程序中的异常

问题描述

我正在尝试创建一个 JavaFX 程序,每次尝试运行我的代码时,我都会遇到异常 - 我不完全确定这意味着什么......

我的代码:

 package com.PickingList.ui;

    import java.io.IOException;

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;

    public class Main extends Application {

        private Stage stage;

        @Override
        public void start(Stage primaryStage) throws IOException {
               Scene scene;
               FXMLLoader loader = new FXMLLoader(this.getClass().getResource("./TelaLogin.fxml"));
               Parent root = loader.load();      
               stage.setScene(new Scene(root));
               stage.setTitle("Login");
               stage.show();

        }

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

我正在使用 Eclipse 创建项目,但出现以下错误:

 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$1(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 com.PickingList.ui.Main.start(Main.java:19)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$4(WinApplication.java:186)
    Exception running application com.PickingList.ui.Main

在此处输入图像描述

标签: javaexceptionjavafxcompiler-errorsruntime-error

解决方案


尝试:

FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/TelaLogin.fxml"));

假设您的文件夹结构如下所示:

/src/TelaLogin.fxml


推荐阅读