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

我正在尝试加载使用场景生成器构建的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$2(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$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.accessController.doPrivileged(accessController.java:389)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(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.access$1600(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

这是我的主要课程代码:

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

dhtz125 回答:当我尝试加载FXML文件时,出现java.io.IOException错误

您导入了错误的ListView

使用您的实际 Exception:

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

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

您的问题不包括MainPageController.java,该错误来自何处,因此我不能确定地回答,但我会去找。

  

侧面说明:我建议您学习Java命名约定并在将来继续使用。

本文链接:https://www.f2er.com/3061641.html

大家都在问