当我尝试加载 FXML 文件时出现 java.io.IOException 错误

When I try to load a FXML file I get a java.io.IOException error

我正在尝试加载我使用场景生成器构建的 fxml 文件,但由于某种原因,我收到 java.io.IOException 错误。

这是我得到的错误:

/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home/bin/java --module-path /Users/anish/codingprojects/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=53528:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/anish/IdeaProjects/FBLA/out/production/FBLA:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx-swt.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.base.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.controls.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.fxml.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.graphics.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.media.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.swing.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.web.jar sample.Main
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: javafx.fxml.LoadException: 
/Users/anish/IdeaProjects/FBLA/out/production/FBLA/sample/mainpage.fxml:35

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at sample.Main.start(Main.java:13)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Caused by: java.lang.IllegalArgumentException: Can not set javax.swing.text.html.ListView field sample.MainPageController.hoursLog to javafx.scene.control.ListView
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.base/java.lang.reflect.Field.set(Field.java:780)
    at javafx.fxml/javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1174)
    at javafx.fxml/javafx.fxml.FXMLLoader.access00(FXMLLoader.java:105)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:865)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:759)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    ... 14 more
Exception running application sample.Main

这是我的主要 class 代码:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;

import java.io.IOException;

public class addHours {
    @FXML
    public void display()
    {
        try {
            Pane addHoursPane = FXMLLoader.load(getClass().getResource("addHours.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是我使用场景生成器构建的 FXML 文件:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.TextFlow?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="518.0" prefWidth="704.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainPageController">
    <children>
        <MenuBar AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <menus>
                <Menu mnemonicParsing="false" text="File">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Close" />
                    </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Delete" />
                        <MenuItem mnemonicParsing="false" text="Unspecified Action" />
                    </items>
                </Menu>
            </menus>
        </MenuBar>
        <SplitPane fx:id="splitPane" dividerPositions="0.23062015503875968" orientation="VERTICAL" prefHeight="518.0" prefWidth="704.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="155.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="28.0">
            <items>
                <AnchorPane maxHeight="300.0" minHeight="100.0" prefHeight="100.0" prefWidth="160.0" />
                <AnchorPane prefHeight="100.0" prefWidth="160.0">
                    <children>
                        <ListView fx:id="hoursLog" layoutX="136.0" layoutY="65.0" prefHeight="393.0" prefWidth="527.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                    </children>
                </AnchorPane>
            </items>
        </SplitPane>
        <Button fx:id="addHoursButton" layoutX="24.0" layoutY="329.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="115.0" text="Add Hours" AnchorPane.bottomAnchor="142.0" />
        <Button fx:id="settings" layoutX="24.0" layoutY="426.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="115.0" text="Settings" AnchorPane.bottomAnchor="82.0" />
        <ImageView fx:id="profilePicture" fitHeight="45.0" fitWidth="45.0" layoutX="1.0" layoutY="29.0" pickOnBounds="true" preserveRatio="true" AnchorPane.leftAnchor="2.0" AnchorPane.topAnchor="30.0" />
        <TextFlow fx:id="profileDetails" layoutX="2.0" layoutY="75.0" prefHeight="35.0" prefWidth="101.0" AnchorPane.leftAnchor="2.0" AnchorPane.topAnchor="77.0" />
    </children>
</AnchorPane>

而here是我的项目结构 (从来没有制作资源选项卡,因为那时我无法将我的控制器与我的 fxml 文件连接) 我刚开始使用 JavaFX,所以我可能在这里看不到一些常见的东西,但我已经在网上到处研究了,似乎没有什么可以解决的。 顺便说一句,我正在使用 JavaFX 11.02、Java SDK 12 和 IntelliJ IDE

您导入错误 ListView

看看你的 实际 Exception:

IllegalArgumentException: Can not set javax.swing.text.html.ListView field sample.MainPageController.hoursLog to javafx.scene.control.ListView

通常这意味着您的 FXML 正在定义一个 JavaFX 控件,但您的实际 class 正在导入 javax.swing.text.html.ListView 而不是 javafx.scene.control.ListView

你的问题不包括MainPageController.java,这个错误是从哪里来的,所以我不能肯定地回答,但我会看看那里。

Side Note: I do recommend learning the Java naming conventions and sticking to them in the future.