JavaFX 8:javafx.fxml.LoadException - fxmlLoader.load() 不起作用,但 fxmlLoader.getController() 起作用
Posted
技术标签:
【中文标题】JavaFX 8:javafx.fxml.LoadException - fxmlLoader.load() 不起作用,但 fxmlLoader.getController() 起作用【英文标题】:JavaFX 8: javafx.fxml.LoadException - fxmlLoader.load() doesn't work, but fxmlLoader.getController() does 【发布时间】:2015-05-29 10:24:55 【问题描述】:尝试从 FXML 文件加载控制器时出现 LoadException。这是 FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Interface.ChangeProjoOrderController">
<children>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<ScrollPane prefHeight="200.0" prefWidth="200.0">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0">
<children>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
</AnchorPane>
</content>
</ScrollPane>
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<Button mnemonicParsing="false" text="Button" />
<Button mnemonicParsing="false" text="Button" />
</children>
</VBox>
</children>
</HBox>
<ToolBar prefHeight="40.0" prefWidth="200.0">
<items>
<Button mnemonicParsing="false" text="Button" />
<Button mnemonicParsing="false" text="Button" />
<Button mnemonicParsing="false" text="Button" />
<Button mnemonicParsing="false" text="Button" />
</items>
</ToolBar>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<Button mnemonicParsing="false" text="Button" />
<Button mnemonicParsing="false" text="Button" />
</children>
</HBox>
</children>
</VBox>
加载FXML的Window是:
package Interface;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import Engine.PackageLight.Projector;
public class ChangeProjoOrderWindow
private MainWindowController mainController;
private FXMLLoader fxmlLoader = null;
private VBox root = null;//TODO uncomment me
//private Parent root = null;//TODO delete me
private ChangeProjoOrderController controller;
private Scene scene;
private Stage stage;
private static final Logger logUI = Logger.getLogger("UI");
ChangeProjoOrderWindow(MainWindowController c)
mainController = c;
try
fxmlLoader = new FXMLLoader(getClass().getResource("fxml/projectorOrder.fxml"));
root = (VBox)fxmlLoader.load();//TODO uncomment me
//root = (Parent)fxmlLoader.load();//TODO delete me
//XXX DO NOT do this !! The controller will stay null (uninitialized)
//root = FXMLLoader.load(getClass().getResource("fxml/orderProjo.fxml"));
catch(IOException e)
logUI.severe("Error: Cannot load the FXML file 'projectorOrder.fxml'.");
e.printStackTrace();
controller = fxmlLoader.<ChangeProjoOrderController>getController();
if(controller == null)
System.err.println("controller is null");
scene = new Scene(root);
System.exit(0);
stage = new Stage();
stage.setOnCloseRequest(we ->
// Application termination event
c.setChangeProjectorOrderDisableStatus(false);
stage.close();
logUI.fine("orderProjo terminated.");
);
mainController.windows.executePreChangeProjectorOrder();
stage.setTitle("Modifier l'ordre des projecteurs");
stage.setScene(scene);
stage.show();
controller.setMainController(mainController);
controller.setStage(stage);
protected void setProjectors(ArrayList<Projector> projectors)controller.setProjectors(projectors);
这里是控制器:
package Interface;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import Engine.PackageLight.Projector;
public class ChangeProjoOrderController implements Initializable
/*@FXML private Button up;
@FXML private Button down;
@FXML private Button newProjo;
@FXML private Button remove;
@FXML private Button duplicate;
@FXML private Button edit;
@FXML private Button ok;
@FXML private Button cancel;
@FXML private GridPane gridPane;*/
private MainWindowController mainController;
private Stage stage;
private ArrayList<Projector> projectorList = null;
private static final Logger logUI = Logger.getLogger("UI");
@Override
public void initialize(URL location, ResourceBundle resources)
/*cancel.setOnAction(event ->
logUI.info("cancel");
mainController.setChangeProjectorOrderDisableStatus(false);
stage.close();
);
ok.setOnAction(event ->
logUI.info("ok");
mainController.setChangeProjectorOrderDisableStatus(false);
stage.close();
);*/
initProjectors();
private void initProjectors()
for(int i=0; i<projectorList.size(); i++)
Label curr = new Label(Integer.toString(i));
GridPane.setColumnIndex(curr, 0);
GridPane.setRowIndex(curr, i);
protected void setMainController(MainWindowController c)mainController = c;
protected void setStage(Stage s)stage = s;
protected void setProjectors(ArrayList<Projector> projectors)projectorList = projectors;
最后,堆栈跟踪:
INFO [UI] Interface.Windows - changeProjoOrder
Changing the order of the projectors...
[0m[31m[1mSEVERE [UI] Interface.ChangeProjoOrderWindow - <init>
Error: Cannot load the FXML file 'projectorOrder.fxml'.
[0mjavafx.fxml.LoadException:
/home/filou/workspace/java/syncart/bin/Interface/fxml/projectorOrder.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413)
at Interface.ChangeProjoOrderWindow.<init>(ChangeProjoOrderWindow.java:35)
at Interface.Windows.changeProjoOrder(Windows.java:151)
at Interface.MainWindowController.changeProjoOrder(MainWindowController.java:308)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1767)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$341(ContextMenuContent.java:1358)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$$Lambda$398/1949840625.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3758)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$357/365619965.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/1327763628.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at Interface.ChangeProjoOrderController.initProjectors(ChangeProjoOrderController.java:58)
at Interface.ChangeProjoOrderController.initialize(ChangeProjoOrderController.java:53)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
... 61 more
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$341(ContextMenuContent.java:1358)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$$Lambda$398/1949840625.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3758)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$357/365619965.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/1327763628.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1767)
... 46 more
Caused by: java.lang.NullPointerException: Root cannot be null
at javafx.scene.Scene.<init>(Scene.java:336)
at javafx.scene.Scene.<init>(Scene.java:194)
at Interface.ChangeProjoOrderWindow.<init>(ChangeProjoOrderWindow.java:50)
at Interface.Windows.changeProjoOrder(Windows.java:151)
at Interface.MainWindowController.changeProjoOrder(MainWindowController.java:308)
... 56 more
好的。现在问题似乎出在这条线上
root = (VBox)fxmlLoader.load();//TODO uncomment me
这会导致 IOException。但之后,线条
controller = fxmlLoader.<ChangeProjoOrderController>getController();
if(controller == null)
System.err.println("controller is null");
不要打印“控制器为空”。因此控制器已正确加载,但“根”未正确加载。我检查了“@FXML”标签(我最后注释掉了......无济于事),FXML文件中的“fx:controller”(它与另一个有效的文件相同,即Interface.xxx ),我从头开始使用 SceneBuilder 2.0 重新制作了窗口,以确保 FXML 生成良好,我尝试将“根”设置为“父”或“VBox”,并将我的文件与其他文件进行比较.
构建的窗口不出现。
我正在使用: LUbuntu 3.13.0-46-通用, JavaFX 8 u40, Eclipse Mars Milestone 3 (4.5.0M3) Build id 20141113-0320, JavaFx SceneBuilder 2.0-b20
那么为什么我有这个例外?哪里来的?
我不确定这是否清楚,我在这段代码中已经有很长时间了:P 如果需要更多信息,请告诉我;我会提供的。
谢谢
【问题讨论】:
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
也许这行得通。顺便说一句,您是否使用场景构建器制作了 UI?因为如果你这样做了,那么你不必在 ChangeProjoOrderWindow
类中声明 VBox
,所有这些都只会在 .fxml 文件中。
是的,我使用 SceneBuilder 2 构建了界面。调用 fxmlLoader = new FXMLLoader(getClass().getResource("fxml/projectorOrder.fxml"));是正确的,因为我使用“fxml”文件夹来存储 FXML 文件。我已经尝试过你的解决方案,但没有奏效。不过,我找到了解决方案。见下文。感谢您的帮助!
【参考方案1】:
堆栈跟踪表明异常来自ChangeProjoOrderController
的第 58 行。
我猜这是
for(int i=0; i<projectorList.size(); i++)
这是有道理的,因为你初始化了
private ArrayList<Projector> projectorList = null;
并且不要更改它的值,至少在加载 FXML 之后(并调用 initalize()
方法)。
【讨论】:
好的,我发现了错误。感谢您的提示!我正在调用一个调用“ChangeProjoOrderWindow”中的方法的方法。问题是,最初的调用是从“ChangeProjoOrderWindow”的构造函数中进行的,所以还没有创建对象!现在,我在构造函数完成后调用这个方法(所以不是从构造函数),它可以工作!再次感谢您的帮助!以上是关于JavaFX 8:javafx.fxml.LoadException - fxmlLoader.load() 不起作用,但 fxmlLoader.getController() 起作用的主要内容,如果未能解决你的问题,请参考以下文章
JavaFX 8:javafx.fxml.LoadException - fxmlLoader.load() 不起作用,但 fxmlLoader.getController() 起作用