在 Java 8 中将控制器添加到 fxml 文件时出现 LoadException
LoadException on adding a controller to fxml file in Java 8
每次我将控制器添加到 fxml 文件时,都会出现异常:javafx.fxml.LoadException,当我打印异常消息时,它会显示文件的正确目录
我正在使用 netbeans 8 IDE、Java 8 和 GUI 的场景构建器
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="419.0" prefWidth="875.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="organizer.Controller.DeleteCategoryController">
<children>
<Pane layoutX="9.0" layoutY="108.0" prefHeight="283.0" prefWidth="550.0" style="-fx-border-width: 1pt; -fx-border-color: black;">
<children>
<Text fx:id="textDeleteThen" layoutX="195.0" layoutY="27.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Delete ... category then" />
<Button layoutX="14.0" layoutY="166.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 3 Create a new Relationship and move contacts having ..... relationship to this relationship" />
<Button layoutX="266.0" layoutY="49.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 2 Keep all contacts with the .... relationship" />
</children></Pane>
<Pane layoutX="569.0" layoutY="19.0" prefHeight="372.0" prefWidth="272.0" style="-fx-border-width: 1pt; -fx-border-color: black;" />
<Text layoutX="41.0" layoutY="73.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Choose one of the following options" wrappingWidth="447.681640625" />
<Text fx:id="textDeletingCategory" layoutX="42.0" layoutY="24.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Deleting ..." />
<Text fx:id="textMessage" layoutX="42.0" layoutY="48.0" strokeType="OUTSIDE" strokeWidth="0.0" text="After deleting .... do the following:" />
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="279.0" layoutY="271.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 4 Change Contacts having .... relationship to the following existing relationship" />
<Button layoutX="23.0" layoutY="153.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 1 Delete all contacts with the ..... relationship" />
<TextField layoutX="585.0" layoutY="56.0" prefHeight="25.0" prefWidth="184.0" />
<ListView fx:id="listViewRemainingCategories" layoutX="580.0" layoutY="138.0" prefHeight="236.0" prefWidth="241.0" />
<Text layoutX="580.0" layoutY="115.0" strokeType="OUTSIDE" strokeWidth="0.0" text="List of other categories (for option 4)" />
<Text layoutX="585.0" layoutY="39.0" strokeType="OUTSIDE" strokeWidth="0.0" text="New Category Name (for option 3)" />
</children>
</AnchorPane>
和控制器:
package organizer.Controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.text.Text;
import organizer.Model.SQL_Objects;
public class DeleteCategoryController extends SQL_Objects implements Initializable{
@FXML private ListView listViewRemainingCategories;
@FXML private Text textDeletingCategory;
@FXML private Text textMessage;
@FXML private Text textDeleteThen;
private int toRemoveId;
private String toRemoveName;
private int associatedRecordsCount;
public DeleteCategoryController(String toRemoveName, int toRemoveId, int associatedRecordCount) {
this.toRemoveName = toRemoveName;
this.toRemoveId = toRemoveId;
this.associatedRecordsCount = associatedRecordCount;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
我程序中的所有其他 FXML 文件都运行良好,它只是这个 FXML 文件,无论我向其中添加哪个控制器,它都会阻止它加载,当我删除它正常打开的控制器属性时。
完整堆栈跟踪:
javafx.fxml.LoadException:
file:/D:/Personal/Projects/Tools/Java/Organizer/dist/run798015614/Organizer.jar!/organizer/View/DeleteCategory.fxml:10
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access0(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at organizer.Controller.Main.switchWindow(Main.java:32)
at organizer.Controller.Main.start(Main.java:21)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait4(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null2(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater3(PlatformImpl.java:294)
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$null7(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.InstantiationException: organizer.Controller.DeleteCategoryController
at java.lang.Class.newInstance(Class.java:427)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
... 23 more
Caused by: java.lang.NoSuchMethodException: organizer.Controller.DeleteCategoryController.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 25 more
知道为什么吗?
感谢您的帮助
所以我找到了解决方案,我将 post 放在这里,以便有人可以利用它
当我删除程序运行的构造函数时,看起来在 JavaFx 应用程序中您只能拥有默认构造函数,但是如果您需要向构造函数添加参数,则需要使用所谓的 ControllerFactory。
每次我将控制器添加到 fxml 文件时,都会出现异常:javafx.fxml.LoadException,当我打印异常消息时,它会显示文件的正确目录
我正在使用 netbeans 8 IDE、Java 8 和 GUI 的场景构建器
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="419.0" prefWidth="875.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="organizer.Controller.DeleteCategoryController">
<children>
<Pane layoutX="9.0" layoutY="108.0" prefHeight="283.0" prefWidth="550.0" style="-fx-border-width: 1pt; -fx-border-color: black;">
<children>
<Text fx:id="textDeleteThen" layoutX="195.0" layoutY="27.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Delete ... category then" />
<Button layoutX="14.0" layoutY="166.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 3 Create a new Relationship and move contacts having ..... relationship to this relationship" />
<Button layoutX="266.0" layoutY="49.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 2 Keep all contacts with the .... relationship" />
</children></Pane>
<Pane layoutX="569.0" layoutY="19.0" prefHeight="372.0" prefWidth="272.0" style="-fx-border-width: 1pt; -fx-border-color: black;" />
<Text layoutX="41.0" layoutY="73.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Choose one of the following options" wrappingWidth="447.681640625" />
<Text fx:id="textDeletingCategory" layoutX="42.0" layoutY="24.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Deleting ..." />
<Text fx:id="textMessage" layoutX="42.0" layoutY="48.0" strokeType="OUTSIDE" strokeWidth="0.0" text="After deleting .... do the following:" />
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="279.0" layoutY="271.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 4 Change Contacts having .... relationship to the following existing relationship" />
<Button layoutX="23.0" layoutY="153.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 1 Delete all contacts with the ..... relationship" />
<TextField layoutX="585.0" layoutY="56.0" prefHeight="25.0" prefWidth="184.0" />
<ListView fx:id="listViewRemainingCategories" layoutX="580.0" layoutY="138.0" prefHeight="236.0" prefWidth="241.0" />
<Text layoutX="580.0" layoutY="115.0" strokeType="OUTSIDE" strokeWidth="0.0" text="List of other categories (for option 4)" />
<Text layoutX="585.0" layoutY="39.0" strokeType="OUTSIDE" strokeWidth="0.0" text="New Category Name (for option 3)" />
</children>
</AnchorPane>
和控制器:
package organizer.Controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.text.Text;
import organizer.Model.SQL_Objects;
public class DeleteCategoryController extends SQL_Objects implements Initializable{
@FXML private ListView listViewRemainingCategories;
@FXML private Text textDeletingCategory;
@FXML private Text textMessage;
@FXML private Text textDeleteThen;
private int toRemoveId;
private String toRemoveName;
private int associatedRecordsCount;
public DeleteCategoryController(String toRemoveName, int toRemoveId, int associatedRecordCount) {
this.toRemoveName = toRemoveName;
this.toRemoveId = toRemoveId;
this.associatedRecordsCount = associatedRecordCount;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
我程序中的所有其他 FXML 文件都运行良好,它只是这个 FXML 文件,无论我向其中添加哪个控制器,它都会阻止它加载,当我删除它正常打开的控制器属性时。
完整堆栈跟踪:
javafx.fxml.LoadException:
file:/D:/Personal/Projects/Tools/Java/Organizer/dist/run798015614/Organizer.jar!/organizer/View/DeleteCategory.fxml:10
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access0(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at organizer.Controller.Main.switchWindow(Main.java:32)
at organizer.Controller.Main.start(Main.java:21)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait4(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null2(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater3(PlatformImpl.java:294)
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$null7(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.InstantiationException: organizer.Controller.DeleteCategoryController
at java.lang.Class.newInstance(Class.java:427)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
... 23 more
Caused by: java.lang.NoSuchMethodException: organizer.Controller.DeleteCategoryController.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 25 more
知道为什么吗? 感谢您的帮助
所以我找到了解决方案,我将 post 放在这里,以便有人可以利用它 当我删除程序运行的构造函数时,看起来在 JavaFx 应用程序中您只能拥有默认构造函数,但是如果您需要向构造函数添加参数,则需要使用所谓的 ControllerFactory。