javafx.fxml.LoadException:已指定根值

时间:2017-08-04 03:54:00

标签: java javafx

我使用SceneBuilder表示JavaFX页面,使用NetBeans表示java代码,包括控制器代码。我有几个控制器,所有控制器都加载了一个加载JavaFX页面的代码。它们都有效,除了1页。

以下是加载页面的方法的代码段:

public boolean showAddProductDialog() 
{
    try 
    {
        // Load the fxml file and create a new stage for the popup dialog.
        FXMLLoader loader = new FXMLLoader();

        loader.setLocation(FXMLInventoryWizard.class.getResource("AddProductScreen.fxml"));

        AnchorPane page = (AnchorPane) loader.load();

        System.out.println("page identifier points to loader.load  line 227");
        // Give the controller access to the main app.
       AddProductController controller = loader.getController();


        controller.setFXMLInventoryWizard(this);

        // Create the dialog Stage.
        Stage dialogStage = new Stage();
        dialogStage.setTitle("Add Product");
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.initOwner(primaryStage);
        Scene scene = new Scene(page);
        dialogStage.setScene(scene);

        // Set the person into the controller.
        AddProductController controller1= loader.getController();
        controller1.setDialogStage(dialogStage);
        controller1.setFXMLInventoryWizard(this);
        // Show the dialog and wait until the user closes it
        dialogStage.showAndWait();

       // return controller.isOkClicked();
       return true;
    } catch (IOException e) {
        System.out.println("there was an IO Exception");
        e.printStackTrace();
        return false;
    }
}

Here's a code snipet of the one that doesn't load:

public boolean showModifyProductDialog() 
{
    try 
    {
        // Load the fxml file and create a new stage for the popup dialog.
        FXMLLoader loader1 = new FXMLLoader();
        System.out.println("we instantiated a FXMLLoader");
        //  loader1.setRoot(ModifyProductController.ModProdPageAnchorPane);
        loader1.setLocation(FXMLInventoryWizard.class.getResource("ModifyProductScreen.fxml"));
        System.out.println("loader.load is " + loader1.load().toString());
        AnchorPane page1 = (AnchorPane) loader1.load();
        System.out.println("did page get assigned to loader.load?");
        // Give the controller access to the main app.
        ModifyProductController controller = loader1.getController();
        controller.setFXMLInventoryWizard(this);

        // Create the dialog Stage.
        Stage dialogStage = new Stage();
        dialogStage.setTitle("Modify Product");
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.initOwner(primaryStage);
        Scene scene = new Scene(page1);
        dialogStage.setScene(scene);

        // Set the person into the controller.
        ModifyProductController controller1= loader1.getController();
        controller1.setDialogStage(dialogStage);
        controller1.setFXMLInventoryWizard(this);
        // Show the dialog and wait until the user closes it
        dialogStage.showAndWait();

       // return controller.isOkClicked();
       return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}   

这里是堆栈跟踪,包括我自己的调试代码的几行(在堆栈跟踪之前):

in handleModifyProduct method of main page controller
we instantiated a FXMLLoader
in the constructor of ModifyProducttController
in the initialize method of ModifyProductContoller
loader.load is AnchorPane[id=AnchorPane]
javafx.fxml.LoadException: Root value already specified.
file:/C:/Users/altered-chords/Desktop/FXMLInventoryWizard/dist/run164609179/FXMLInventoryWizard.jar!/fxmlinventorywizard/ModifyProductScreen.fxml
file:/C:/Users/altered-chords/Desktop/FXMLInventoryWizard/dist/run164609179/FXMLInventoryWizard.jar!/fxmlinventorywizard/ModifyProductScreen.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2755)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2704)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at fxmlinventorywizard.FXMLInventoryWizard.showModifyProductDialog(FXMLInventoryWizard.java:268)
    at fxmlinventorywizard.FXMLDocumentController.handleModifyProduct(FXMLDocumentController.java:269)
    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:498)
    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:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    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.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.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.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    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.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:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

我用跳蚤梳子检查了代码。比较两个snipets逐行。第4次将工作代码复制到非工作部分并进行编辑。这次它奏效了。看起来我的架构缺乏。太多相同的代码。我有很多要学习的。谢谢大家。