【问题标题】:How to repair Caused by: java.lang.NullPointerException: Location is required. JavaFX如何修复原因:java.lang.NullPointerException:需要位置。 JavaFX
【发布时间】:2019-03-28 11:52:07
【问题描述】:

您好,将 maven 添加到我的 JavaFX 模块后发生崩溃,无法找到解决方法。我真的尝试了很多来自 stackof 的解决方案,但没有任何效果。在我的 start() 方法中,intellij 找不到我的 fxml 文件的资源,当我在同一目录中创建其他 fxml 文件时,它也总是返回 null。这真的很奇怪,因为当我使用 CTRL+单击 second.fxml intellij 正确查找文件时。一切都在 Maven 之前工作。

试过了:

- getClass().getResource("second.fxml")
- getClass().getClassLoader().getResource("second.fxml")
- System.out.println("Test  = "+Main.class.getClassLoader().getResource("second.fxml")); <- always null
- getClass().getClassLoader().getResource("src/java/sample/second.fxml")
And a lot of other combine with getClass().

主类

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 {


    @Override
    public void start(Stage primaryStage) throws Exception{
        System.out.println("Test  = "+Main.class.getClassLoader().getResource("second.fxml"));
        Parent root = FXMLLoader.load(getClass().getResource("second.fxml"));
        primaryStage.setTitle("Hello");
        primaryStage.setScene(new Scene(root, 800, 800));
        primaryStage.show();

    }


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

FXML 类

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="pl.zar.fx.Controller"
            prefHeight="400.0" prefWidth="600.0">

</AnchorPane>

控制器类

public class Controller {

@FXML
    public void onClickAddButton(ActionEvent event){}
}

还有我的 Maven pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>javafxapp</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <dependencies> <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency></dependencies>
</project>

错误代码:

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:748)
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 pl.zar.fx.Main.start(Main.java:15)
    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 pl.zar.fx.Main

我希望我的 fxml 模板返回,但总是得到 NULL。 编辑项目结构: project structure

【问题讨论】:

标签: maven javafx


【解决方案1】:

我建议将您的fxml 文件放入resources 文件夹并使用以下代码调用它们:

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("second.fxml"));

这里重要的是您需要将fxml 文件移动到资源文件夹中。否则将无法正常工作。

也记录在this 答案中。

【讨论】:

  • 我试过这个解决方案,抛出同样的错误我也试过把 fxml 到 src 到 main 到 java 到其他包,给出完整路径,部分路径只有 fxml 名称等,但总是返回 NULL
【解决方案2】:

好的,我解决了: 正如你所说,我将 fxml 文件移动到资源并切换

Parent root = FXMLLoader.load(getClass().getResource("second.fxml"));

Parent root = FXMLLoader.load(Main.class.getClassLoader().getResource("second.fxml"));

现在工作得很好,我不知道为什么我必须使用对 Main 类的引用。在添加 maven 框架之前一切都很好。

【讨论】:

  • 请阅读 class.getResource 和 classLoader.getResource 的 api 文档:它们在查找资源的位置不同。
猜你喜欢
  • 2019-03-26
  • 2017-04-16
  • 1970-01-01
  • 2016-09-25
  • 2014-11-13
  • 2014-03-26
  • 1970-01-01
  • 2014-02-24
相关资源
最近更新 更多