我正在尝试编写一个简单的文字处理器,并且我有一个控制器可以与多个 FXML 文件进行通信。与打开以前保存的文档相比,为新文档使用不同的 FXML 表示似乎最简单,后者是出现问题的视图。两个 FXML 文件都在其根节点中标识了控制器。其中一个(我创建的第一个,用于新文件的)工作正常。另一个,无论我尝试过什么,每当我尝试从中访问节点时都会抛出 NullPointerException。我已确保我尝试引用的所有元素都使用 @FXML 进行修饰。
这是我的 FXML 的相关部分:
openDocView
<BorderPane fx:controller="sample.MenuController"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:id="opened_doc_view">
<center>
<ScrollPane prefWidth="400" prefHeight="400">
<padding>
<Insets top="30" left="170" right="5" bottom="30"/>
</padding>
<GridPane gridLinesVisible="true" fx:id="pages_grid_opening">
<TextArea GridPane.columnIndex="0" GridPane.rowIndex="0" prefHeight="700" prefWidth="400" wrapText="true" fx:id="text_area_opening"></TextArea>
</GridPane>
</ScrollPane>
</center>
</BorderPane>
菜单
<BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="sample.MenuController"
prefHeight="700.0" prefWidth="700.0"
fx:id="menu_borderpane">
<center>
<ListView fx:id="documentsListView"></ListView>
</center>
<right>
<VBox fx:id="menuOptions" prefHeight="700.0">
<Button fx:id="new_button_menu" text="New"/>
<Button fx:id="open_button" text="Open"/>
<Button fx:id="search_button" text="Search"/>
</VBox>
</right>
</BorderPane>
来自控制器:
menuController
@FXML
private BorderPane opened_doc_view;
@FXML
private Button new_button_menu;
@FXML
private TextArea text_area_opening;
public void initialize() throws IOException{
text_area_opening = (TextArea) getNodeByIndex(pages_grid_opening, 0, 0);
new_button_menu.addEventHandler(MouseEvent.MOUSE_RELEASED, (event)->{
//do some stuff
}
堆栈跟踪:
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:566)
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:566)
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:829)
Caused by: javafx.fxml.LoadException:
/C:/Users/phdav/IdeaProjects/pace3/out/production/pace3/sample/Menu.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
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 pace3/sample.Main.start(Main.java:27)
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(Native Method)
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)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: 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:566)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2591)
... 17 more
Caused by: java.lang.NullPointerException
at pace3/sample.MenuController.getNodeByIndex(MenuController.java:90)
at pace3/sample.MenuController.initialize(MenuController.java:51)
... 28 more
Exception running application sample.Main
此外,我之前也曾因提问礼仪不佳而受到批评,所以请告诉我如何改进这一点。