java.lang.IllegalStateException : Location is not set

标签 java maven javafx fxml

我收到以下错误。我已附上我的代码。

Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at com.John.Smith.Cars.Main.start(Main.java:31)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
... 1 more

这是我的主类(class)。

package com.John.Smith.Cars;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.Date;
import java.util.Random;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        System.out.println(System.getProperty("java.version"));

        //PushNPull p = new PushNPull();
        //p.populateSQL();

        StageWithData window = new StageWithData(primaryStage);
        FXMLLoader hpLoader = new FXMLLoader(getClass().getResource("/homePage.fxml"));



        System.out.println(hpLoader.getController() + ", " + hpLoader.getLocation());
        Parent root = hpLoader.load(); //This is the line with the error
        ControllerHomePage controller = hpLoader.getController();
        controller.setStage(window);

        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("/styles.css").toExternalForm());
        window.setScene(scene);
        window.show();

        new Thread(() -> {
                abstractSensorDriver sDriver = new SensorDriverTest(window);
                sDriver.startCollection();
        }).start();
    }


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

}

我看过其他帖子也有同样的错误,但似乎仍然不起作用。 JAVAFX: Location is not set error "java.lang.IllegalStateException: Location is not set" in JavaFX application

编辑:这是我的 homePage.fxml 的前几行:

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

<?import javafx.scene.shape.*?>
<?import javafx.scene.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<GridPane alignment="center" hgap="10" stylesheets="@styles.css" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.John.Smith.Cars.ControllerHomePage">
    <rowConstraints>
        <RowConstraints />
        <RowConstraints />
    </rowConstraints>

我的 FXML 文件与我的主类位于同一文件夹中。 我在这个项目中使用 Maven(我的第一个 Maven 项目)。

运行Main方法后。我的加载器位置的 println() 打印出“Null , Null”

最佳答案

如果文件与调用加载器的类位于同一文件夹中,则文件路径中不应包含 /

将行更改为:

FXMLLoader hpLoader = new FXMLLoader(getClass().getResource("homePage.fxml"));

您正在尝试设置加载程序以在根项目文件夹中查找该文件。

关于java.lang.IllegalStateException : Location is not set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51128560/

相关文章:

java - 删除 Oracle Java SE 的商业组件

java - Spring Integration从动态多个目录读取文件

java - Maven Jaxb 插件无法从目标文件夹生成类

java - 什么时候应该在项目生命周期中使用 mvn release?

java - 如何在 javaFx java8 中禁用 textArea 自动滚动

java - 单独的集合类对象

java - Maven - 依赖于多模块聚合器项目

java - 使用 java/javafx 更改 Linux 中的 Activity/应用程序菜单栏

java - 如何在 Swing 应用程序中更新 JavaFX BarChart?

java - 解析 GSON Google Places API