为什么我得到javafx.fxml.LoadException甚至fxml文件的路径都是正确的 [英] Why I'm getting javafx.fxml.LoadException even the path of the fxml file is correct

查看:204
本文介绍了为什么我得到javafx.fxml.LoadException甚至fxml文件的路径都是正确的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到


javafx.fxml.LoadException:

javafx.fxml.LoadException:

当我使用以下代码行加载和fxml文件时。

When I'm loading and fxml file using following line of code.


AnchorPane anchorPane =(AnchorPane)加载程序。 load()

AnchorPane anchorPane = (AnchorPane)loader.load()

这是我的fxml文件,不包括import语句。

This is my fxml file excluding import statements.

<AnchorPane prefHeight="537.0" prefWidth="374.0"     xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.buddhikajay.controller.NewTransactionDialogController">
   <children>
      <GridPane hgap="5.0" layoutX="30.0" layoutY="10.0" prefHeight="544.0" prefWidth="314.0" vgap="5.0">
    <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="142.0" minWidth="10.0" prefWidth="69.0" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="226.0" minWidth="10.0" prefWidth="225.0" />
    </columnConstraints>
    <rowConstraints>
      <RowConstraints maxHeight="70.0" minHeight="10.0" prefHeight="33.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="105.0" minHeight="10.0" prefHeight="38.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="156.0" minHeight="10.0" prefHeight="51.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="154.0" minHeight="10.0" prefHeight="43.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="160.0" minHeight="10.0" prefHeight="43.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="161.0" minHeight="10.0" prefHeight="63.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="301.0" minHeight="10.0" prefHeight="180.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="231.0" minHeight="10.0" prefHeight="106.0" vgrow="SOMETIMES" />
    </rowConstraints>
     <padding>
        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
     </padding>
     <children>
        <Label text="ID" />
        <Label text="Date" GridPane.rowIndex="1" />
        <Label text="Amont" GridPane.rowIndex="2" />
        <Label text="Person" GridPane.rowIndex="4" />
        <Label text="Type" GridPane.rowIndex="3" />
        <Label text="Resolved" GridPane.rowIndex="5" />
        <Label />
        <Label text="Description" GridPane.rowIndex="6" />
        <TextField fx:id="idTextField" GridPane.columnIndex="1" />
        <TextField fx:id="dateTextField" GridPane.columnIndex="1" GridPane.rowIndex="1" />
        <TextField fx:id="amountTextField" GridPane.columnIndex="1" GridPane.rowIndex="2" />
        <TextArea fx:id="descriptionTextArea" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="6" />
        <HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="50.0" prefWidth="225.0" spacing="20.0" GridPane.columnIndex="1" GridPane.rowIndex="7">
           <GridPane.margin>
              <Insets />
           </GridPane.margin>
           <padding>
              <Insets bottom="10.0" left="20.0" right="10.0" top="10.0" />
           </padding>
           <children>
              <Button fx:id="okButton" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#okActionFired" prefHeight="25.0" prefWidth="75.0" text="Ok" />
              <Button fx:id="cancleButton" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#cancleActionFired" prefHeight="25.0" prefWidth="75.0" text="Cancle" />
           </children>
        </HBox>
        <ComboBox fx:id="typeComboBox" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
        <ComboBox fx:id="personComboBox" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="4" />
        <ComboBox fx:id="resolvedComboBox" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="5" />
     </children>
  </GridPane>
  </children>
</AnchorPane>

编译器抱怨行号1有错误。我确定路径是fxml文件是正确的。我的代码出了什么问题?

The compiler complains that there is an error in line number 1. I'm sure that the path of fxml file is correct. What is wrong with my code?

这是完整的错误堆栈

javafx.fxml.LoadException: 
/C:/Users/Buddhika/Documents/Programming/IdeaProjects/VirtualCreditDesktop/out/production/VirtualCreditDesktop/com/buddhikajay/view/NewTransactionDialog.fxml:9

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
    at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:104)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:928)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:967)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:216)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:740)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2701)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2521)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
    at com.buddhikajay.controller.NewTransactionDialogController.showNewTransactionDialog(NewTransactionDialogController.java:68)
    at com.buddhikajay.controller.MainApp.start(MainApp.java:51)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1976027283.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/301541210.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/474926796.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1662429848.run(Unknown Source)
    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$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$38/519284171.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.InstantiationException: com.buddhikajay.controller.NewTransactionDialogController
    at java.lang.Class.newInstance(Class.java:423)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:923)
    ... 23 more
Caused by: java.lang.NoSuchMethodException: com.buddhikajay.controller.NewTransactionDialogController.<init>()
    at java.lang.Class.getConstructor0(Class.java:3074)
    at java.lang.Class.newInstance(Class.java:408)
    ... 25 more


推荐答案

问题在堆栈跟踪中描述:

The problem is described in the stack trace:

Caused by: java.lang.NoSuchMethodException: com.buddhikajay.controller.NewTransactionDialogController.()

基本上说你的控制器class没有零参数构造函数。

which basically says your controller class doesn't have a zero-argument constructor.

默认情况下, FXMLLoader 将通过调用创建控制器它的零参数构造函数。删除在控制器类中定义的构造函数,或删除它所采用的参数。

By default, the FXMLLoader will create the controller by calling its zero-argument constructor. Remove the constructor that you have defined in the controller class, or remove the parameters that it takes.

如果你真的需要将参数传递给控制器​​的构造函数,你可以用Java代码创建控制器并将其传递给FXMLLoader:

If you really need to pass arguments to the controller's constructor, you can either create the controller in Java code and pass it to the FXMLLoader:

NewTransactionDialogController controller = new NewTransactionDialogController(someValue);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(...);
loader.setController(controller);
AnchorPane anchorPane = loader.load();

(使用此代码,您必须删除 fx:controller 属性。)

(Using this code, you must remove the fx:controller attribute from the FXML.)

或者,设置一个控制器工厂:

Or, set a controller factory:

FXMLLoader loader = new FXMLLoader();
loader.setLocation(...);
loader.setControllerFactory(clazz -> {
    if (clazz == NewTransactionDialogController.class) {
        return new NewTransactionDialogController(someValue);
    } else {
        // default behavior:
        try {
            return clazz.newInstance();
        } catch (Exception exc) {
            throw new RuntimeException(exc);
        }
    }
});

AnchorPane anchorPane = loader.load();

(在此版本中,请保留 fx:controller FXML文件中的属性。)

(In this version, leave the fx:controller attribute in the FXML file.)

这篇关于为什么我得到javafx.fxml.LoadException甚至fxml文件的路径都是正确的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆