Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.IllegalStateException:未设置位置_Java_Maven_Javafx_Fxml - Fatal编程技术网

java.lang.IllegalStateException:未设置位置

java.lang.IllegalStateException:未设置位置,java,maven,javafx,fxml,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(

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

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
这是我的主课

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);
    }

}
我见过其他帖子也有同样的错误,但似乎仍然不起作用。

编辑:以下是my 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方法之后。加载程序位置的My println()打印出“Null,Null”

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

将行更改为:

fxmloader hpLoader=newfxmloader(getClass().getResource(“homePage.fxml”)


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

请附加您的
主页.fxml
文件我刚刚附加了我的主页.fxml.Should
getResource(“/com/John/Smith/Cars/homePage.fxml”)
。如果这不起作用,您需要检查fxml在运行时是否在类路径中…当您说“我的fxml文件与我的主类位于同一文件夹中”时,这是否意味着fxml文件位于
src\Main\java
目录中?如果是这样,我相信这是你的问题。Maven不会将资源从
src\main\java
目录复制到输出目录(至少在默认情况下是这样)。相反,将FXML文件移动到
src\main\resources
目录中。这就是资源(即非
.java
文件)所属的地方@沙拉。就这样。我在资源文件夹中没有FXML文件。我把它放在src\main\java目录中。在我创建了一个资源文件夹并将其添加到我的POM文件后,它就工作了。谢谢在使用Maven之前,我已经让它工作了。我将所有文件移到了一个新的Maven项目中,它现在给了我一个错误。我想知道它是否与我的文件夹结构或POM文件有关??