gpt4 book ai didi

JavaFX 8 : javafx. fxml.LoadException - fxmlLoader.load() 不起作用,但 fxmlLoader.getController() 起作用

转载 作者:行者123 更新时间:2023-11-30 08:18:52 26 4
gpt4 key购买 nike

尝试从 FXML 文件加载 Controller 时出现 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的窗口是:

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);}
}

这是 Controller :

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...
SEVERE [UI] Interface.ChangeProjoOrderWindow - <init>
Error: Cannot load the FXML file 'projectorOrder.fxml'.
javafx.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");

不要打印“ Controller 为空”。因此 Controller 已正确加载,但“root”未正确加载。我检查了“@FXML”标签(我最后注释掉了......无济于事),FXML文件中的“fx:controller”(它的编写方式与另一个有效的文件相同,即Interface.xxx) ),我从头开始使用 SceneBuilder 2.0 重新制作了窗口,以确保 FXML 生成良好,我尝试将“root”设置为“Parent”或“VBox”,并将我的文件与其他有效的文件进行了比较.

构建的窗口不会出现。

我正在使用:LUbuntu 3.13.0-46-通用,JavaFX 8 u40,Eclipse Mars Milestone 3 (4.5.0M3) 内部版本号 20141113-0320,JavaFx SceneBuilder 2.0-b20

那么为什么我有这个异常(exception)呢?它从哪里来?

我不确定这是否清楚,我已经使用这段代码很长时间了:P如果需要更多信息,请告诉我;我来提供。

谢谢

最佳答案

堆栈跟踪显示异常来自 ChangeProjoOrderController 的第 58 行。

我猜这是

for(int i=0; i<projectorList.size(); i++)

这是有道理的,因为你初始化了

private ArrayList<Projector> projectorList = null;

并且不要更改其值,至少在加载 FXML 之前(并且调用 initalize() 方法)。

关于JavaFX 8 : javafx. fxml.LoadException - fxmlLoader.load() 不起作用,但 fxmlLoader.getController() 起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29260919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com