java - 应用程序启动方法中的异常

标签 java exception

我正在尝试加载 FXML 文件并将其显示为应用程序窗口,但出现异常。 FXML 文件由 FXML Scene Builder 创建。

sup.java文件:

package sup;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;


public class Sup extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));



        primaryStage.setTitle("Sup");
        Scene scene = new Scene(root, 300, 250);        
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

sample.fxml 文件:

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.control.TextField?>
<GridPane fx:controller="sup.Controller"
    xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="5"> 

    <Text text="Sup"
        GridPane.rowIndex="0"
        GridPane.columnSpan="2"
        GridPane.halignment="CENTER"/>

    <Label text="First Name:"
        GridPane.rowIndex="1"
        GridPane.columnIndex="0"/>

    <TextField
        GridPane.columnIndex="1"
        GridPane.rowIndex="1"/>

    <Button text="Say Sup!"
        GridPane.rowIndex="2"
        GridPane.columnIndex="1"
        GridPane.halignment="RIGHT"/>

</GridPane>

结构截图:

enter image description here

错误信息:

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$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at sup.Sup.start(Sup.java:34)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
    ... 1 more
Exception running application sup.Sup

最佳答案

您遇到的异常的根本原因是 NullPointerException 以及详细消息“需要位置”。

Caused by: java.lang.NullPointerException: Location is required.

我不会指导您如何阅读堆栈跟踪,请参阅 this question相反。

您收到“location is required”错误,因为您将 null 传递给 FXMLLoader.load()方法。 null 值显然必须来自 getResource()称呼。来自Class.getResource()文档:

[...]

Returns:
A URL object or null if no resource with this name is found

[...]

所以你得到异常的原因必须是找不到具有给定名称的资源。

请验证以下内容:

  • 如果您使用标准的 eclipse 项目布局:确保将 sample.fxml 放入与 Sup.java 相同的目录中> 文件
  • 如果您使用的是类似 maven 的目录结构,其中资源和来源的目录不同,请确保 sample.fxml 在资源中文件夹和与源文件相同的目录。

    示例:您的源文件Sup.java 放在src/main/java/com/example/Sup.java 中。将 sample.fxml 文件放入 src/main/resources/com/example/sample.fxml

  • 如果您使用的是 eclipse,请清理并重建您的项目。构建工具有类似的选择(或者直接删除build/out目录)

关于java - 应用程序启动方法中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36250965/

相关文章:

java - 我的测试用例中预期异常的正确组合是什么?

ruby - 引发异常 : string vs custom class

java - 抛出什么异常?

c++ - 捕获在不同线程中运行的方法的异常的正确方法是什么?

java - ListView 不从 OnClickListener 重绘

java - 对二维ArrayList进行排序

java - 使用 jdk 7 新的 fork join 框架的性能问题

java - 为什么当有2个类型名称相似的对象时会出现ClassLoader异常

java - Hibernate延迟加载重复数据

PHPUnit try catch 在测试用例中不起作用