[英]JavaFX exception "Exception in Application start method" only after I add ids and onAction using Scene Builder
我有一個項目,我在 Scene Builder 上創建 GUI。 當我第一次創建我的 GUI 的所有元素時,它工作得很好。 但是,一旦我將 ids 和 onActions 添加到我的 textFields 和其他元素中,它就會破壞代碼,提示“應用程序啟動方法中存在異常”。 我不確定如何處理這個問題,因為我一直在對其他項目做完全相同的事情,而這以前從未發生過。
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ListView fx:id="eventListView" layoutX="239.0" layoutY="60.0" prefHeight="380.0" prefWidth="348.0">
<contextMenu>
<ContextMenu>
<items>
<MenuItem mnemonicParsing="false" text="Details" />
<MenuItem mnemonicParsing="false" text="Remove" />
</items>
</ContextMenu>
</contextMenu>
</ListView>
<TextField fx:id="titleTextField" layoutX="46.0" layoutY="101.0" />
<TextField fx:id="descriptionTextField" layoutX="46.0" layoutY="166.0" />
<DatePicker fx:id="datePickerField" layoutX="40.0" layoutY="238.0" />
<ComboBox fx:id="cmbAddType" layoutX="47.0" layoutY="295.0" onAction="#selectedType" prefWidth="150.0" />
<Button fx:id="addEventButton" layoutX="72.0" layoutY="365.0" mnemonicParsing="false" onAction="#handleAddEvent" prefHeight="30.0" prefWidth="100.0" text="Add" />
<ComboBox fx:id="cmbOrderFilter" layoutX="614.0" layoutY="153.0" onAction="#orderList" prefWidth="150.0" />
<ComboBox fx:id="cmbTypeFilter" layoutX="614.0" layoutY="208.0" onAction="#filterType" prefWidth="150.0" />
<Button fx:id="removeButton" layoutX="648.0" layoutY="257.0" mnemonicParsing="false" onAction="#handleRemoveEvent" prefHeight="30.0" prefWidth="100.0" text="Remove" />
<Label layoutX="109.0" layoutY="74.0" text="Title" />
<Label layoutX="88.0" layoutY="148.0" text="Description" />
<Label layoutX="108.0" layoutY="213.0" text="Date" />
<Label layoutX="108.0" layoutY="272.0" text="Type" />
<Label layoutX="673.0" layoutY="127.0" text="Order" />
<Label layoutX="675.0" layoutY="183.0" text="Type" />
<Label layoutX="381.0" layoutY="25.0" text="Events List" />
</children>
</AnchorPane>
public class MainWindow {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private Button addEventButton;
@FXML
private ComboBox<?> cmbAddType;
@FXML
private ComboBox<?> cmbOrderFilter;
@FXML
private ComboBox<?> cmbTypeFilter;
@FXML
private DatePicker datePickerField;
@FXML
private TextField descriptionTextField;
@FXML
private ListView<?> eventListView;
@FXML
private Button removeButton;
@FXML
private TextField titleTextField;
@FXML
void filterType(ActionEvent event) {
}
@FXML
void handleAddEvent(ActionEvent event) {
}
@FXML
void handleRemoveEvent(ActionEvent event) {
}
@FXML
void orderList(ActionEvent event) {
}
@FXML
void initialize() {
assert this.addEventButton != null : "fx:id=\"addEventButton\" was not injected: check your FXML file 'MainWindow.fxml'.";
assert this.cmbAddType != null : "fx:id=\"cmbAddType\" was not injected: check your FXML file 'MainWindow.fxml'.";
assert this.cmbOrderFilter != null : "fx:id=\"cmbOrderFilter\" was not injected: check your FXML file 'MainWindow.fxml'.";
assert this.cmbTypeFilter != null : "fx:id=\"cmbTypeFilter\" was not injected: check your FXML file 'MainWindow.fxml'.";
assert this.datePickerField != null : "fx:id=\"datePickerField\" was not injected: check your FXML file 'MainWindow.fxml'.";
assert this.descriptionTextField != null : "fx:id=\"descriptionTextField\" was not injected: check your FXML file 'MainWindow.fxml'.";
assert this.eventListView != null : "fx:id=\"eventListView\" was not injected: check your FXML file 'MainWindow.fxml'.";
assert this.removeButton != null : "fx:id=\"removeButton\" was not injected: check your FXML file 'MainWindow.fxml'.";
assert this.titleTextField != null : "fx:id=\"titleTextField\" was not injected: check your FXML file 'MainWindow.fxml'.";
}
}
public class Main extends Application {
private static final String WINDOW_TITLE = "Bulletin Board";
private static final String GUI_RESOURCE = "view/MainWindow.fxml";
/**
* JavaFX entry point.
*
* @precondition none
* @postcondition none
*
* @throws IOException
*/
@Override
public void start(Stage primaryStage) throws IOException {
Parent parent = FXMLLoader.load(getClass().getResource(Main.GUI_RESOURCE));
Scene scene = new Scene(parent);
primaryStage.setTitle(WINDOW_TITLE);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* Primary Java entry point.
*
* @precondition none
* @postcondition none
*
* @param args command line arguments
*/
public static void main(String[] args) {
Main.launch(args);
}
}
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:77 ) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at javafx.graphics@18.0. 1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465) at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364) at java. base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本機方法)位於 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.base/sun.launcher .LauncherHelper$FXHelper.main(LauncherHelper.java:1082) 原因:java.lang.RuntimeException:javafx.graphics@18.0.1/com.sun.883603413301888.graphics@18.0.1/com.sun.883603413301888.application(application) java:901) at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196) at java.base/java.lang.Thread.run(Thread.java :833)引起:javafx.fxml.LoadException:沒有指定controller。 /C:/Users/vvhs1/OneDrive/Documents/UWG/2022/Fall%202022/CS1302/Project3/BulletinBoardRepo/BulletinBoard/bin/edu/westga/cs1302/bulletin_board/view/MainWindow.fxml:28
at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2703) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:568) at javafx .fxml@18.0.1/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:781) at javafx .fxml@18.0.1/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2924) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2639) at javafx.fxml@18.0 .1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331) at javafx.fxml@18.0.1/javafx .fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287) 在 javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadI mpl(FXMLLoader.java:3255) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader. java:3203) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196) at edu.westga.cs1302.bulletin_board.Main.start(Main.java:30) at javafx.graphics @18.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847) 在 javafx.graphics@18.0.1/com.sun.javafx.application.And$WlamitImpl.PlatformImpl2$ PlatformImpl.java:484) at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457) at java.base/java.security.AccessController.doPrivileged(AccessController .java:399) 在 javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$ 11(PlatformImpl.java:456)javafx.graphics@18.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)javafx.ungraphics@1888.ungraphics@11 glass.ui.win.WinApplication._runLoop(本機方法)位於 javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)... 1 更多異常運行應用程序 edu.westga.cs1302.bulletin_board.Main
我試過更改 JavaFX 版本,重新安裝 Scene Builder,但沒有用。 每次我刪除我的 FXML 文件上的所有組件並重建它時,它都會工作,直到我添加 ids 和 onAction。 正如我之前提到的,在過去的幾個月里,我一直在使用場景構建器並這樣做,但這種情況從未發生過。
錯誤消息告訴您出了什么問題:
Caused by: javafx.fxml.LoadException: No controller specified.
您需要將 controller 與 FXML 相關聯(如果您在 FXML 中提供使用 controller 的說明,例如,在 FXML 中設置操作處理程序)。
請參閱有關控制器的 FXML 文檔。
您可以通過在根元素中提供引用來在 FXML 中指定 controller(將 controller 名稱替換為控制器的完全限定名稱):
fx:controller="com.foo.MyController"
SceneBuilder可以協助設置controller:
或者您可以在加載器上設置 controller 。
FXMLLoader loader = new FXMLLoader();
loader.setController(
new MyController()
);
或者你可以使用controller 工廠,它通常與依賴注入框架一起使用,例如 Spring。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.