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

时间:2015-03-25 16:13:59

标签: java javafx-8 fxml

尝试从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的窗口是:

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生成良好,我试图将“root”设置为“Parent”或“VBox”,并将我的文件与其他工作的文件进行比较

不会出现构建的窗口。

我正在使用: LUbuntu 3.13.0-46-generic, JavaFX 8 u40, Eclipse Mars Milestone 3(4.5.0M3)Build id 20141113-0320, JavaFx SceneBuilder 2.0-b20

那为什么我这个例外?它来自哪里?

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

由于

1 个答案:

答案 0 :(得分:1)

堆栈跟踪表明异常来自ChangeProjoOrderController的第58行。

我猜这是

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

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

private ArrayList<Projector> projectorList = null;

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