JavaFX - 为什么抛出java.lang.NullPointerException?

时间:2017-01-16 12:02:31

标签: java javafx fxml

我有一个加载MainView.fxml的舞台。此MainView.fxml包含其他三个视图。在其他一个视图中我有按钮,我可以完全修改而没有问题。

这是我的MainView.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.*?>

<BorderPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MainController">
   <left>
      <Pane prefHeight="800.0" prefWidth="300.0" BorderPane.alignment="CENTER">
         <children>
            <fx:include fx:id="ControlsView" source="ControlsView.fxml" />
            <fx:include fx:id="ParametersView" source="ParametersView.fxml" />
         </children>
      </Pane>
   </left>
    <center>
        <fx:include fx:id="CashRegisterView" source="CashRegisterView.fxml" />
    </center>
</BorderPane>

例如在ControlsController的控制器中,我有一个修改按钮的方法,它可以正常工作:

@FXML
private void btStartActionEventHandler(MouseEvent event){
    if(controlsModel.getBtStartValue() == ControlsModel.buttonStates.Start){
        StartSuperSim();
        controlsModel.setBtStartValue(ControlsModel.buttonStates.Pause);
        btStart.setText(controlsModel.getBtStartValue().toString());
        btStop.setDisable(false);
    }
}

您可以清楚地看到我修改了btStartbtStop的属性。

但是当我尝试修改来自ParametersView.fxml的任何内容时,它会引发java.lang.reflect.InvocationTargetExceptionjava.lang.reflect.InvocationTargetExceptionjava.lang.NullPointerException。如完整stackTrace中所示here

以下是有问题的代码:

public class CashRegisterController extends BaseController{

    @FXML
    private Pane pCashRegister;

    @FXML
    public void CreateCashRegisters(){

        Pane pane = new Pane();
        pane.setStyle("-fx-background-color: black;");

        this.pCashRegister.getChildren().add(0, pane);
    }
}

我没有创建pCashRegister的实例,因为我在ControlsController中也没有这样做。但是当我这样做时,它工作正常,但它不会将pane添加到我的视图中。

@FXML
private Button btStart;
@FXML
private Button btStop;

最后我的CashRegisterView.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.Pane?>

<Pane fx:id="pCashRegister" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.CashRegisterController">

有人可以解释一下这是如何工作的以及这里发生了什么?我似乎无法弄明白。我只是认为,无论出于何种原因CashRegisterView.fxml都没有链接到我的CashRegisterController,即使我指定了fx:controller="controller.CashRegisterController"

修改

StartSuperSim()方法:

private void StartSuperSim(){
    getCashRegisterController().CreateCashRegisters();

    seconds = 0;

    setSuperSim(new Timeline(new KeyFrame(Duration.seconds((1 / controlsModel.getAcceleration())), new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            seconds++;
            //System.out.println(seconds);

            if((seconds % (int)controlsModel.getParametersModel().getTimeBetweenCustomers()) == 0){
                //System.out.println("Fired");
            }
        }
    })));
    getSuperSim().setCycleCount(Timeline.INDEFINITE);
    getSuperSim().play();
}

我的MainController.java

package controller;
public class MainController extends BaseController{

private ControlsController controlsController;
private ParametersController parametersController;
private CashRegisterController cashRegisterController;

public MainController() {
    controlsController = Controller.getInstance().getControlsController();
    parametersController = Controller.getInstance().getParametersController();
    cashRegisterController = Controller.getInstance().getCashRegisterController();
}
}

我用来传递实例的Controller.java类:

package controller;

public class Controller {
private final static Controller instance = new Controller();

public static Controller getInstance() {
    return instance;
}

private ControlsController controlsController = new ControlsController();
private ParametersController parametersController = new ParametersController();
private CashRegisterController cashRegisterController = new CashRegisterController();

public ControlsController getControlsController() {
    return controlsController;
}

public ParametersController getParametersController() {
    return parametersController;
}

public CashRegisterController getCashRegisterController() {
    return cashRegisterController;
}
}

最后是完整的堆栈跟踪:

    "D:\Program Files\Java\jdk1.8.0_102\bin\java" -Didea.launcher.port=7534 "-Didea.launcher.bin.path=D:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.5\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_102\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\rt.jar;D:\This PC\Documents\Projects\Java\SuperSim\out\production\SuperSim;D:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.5\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Main
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    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:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    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:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    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)
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: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:1769)
    ... 31 more
Caused by: java.lang.NullPointerException
    at controller.CashRegisterController.CreateCashRegisters(CashRegisterController.java:26)
    at controller.ControlsController.StartSuperSim(ControlsController.java:95)
    at controller.ControlsController.btStartActionEventHandler(ControlsController.java:69)
    ... 41 more

1 个答案:

答案 0 :(得分:0)

您在CashRegisterController单例中创建和存储的Controller实例与FXMLLoader在加载CashRegisterView.fxml时创建的实例不同。当然,FXMLLoader初始化它创建的实例上的@FXML - 注释字段(它不能访问存储在单例中的实例,除非你有一个非常复杂的控制器工厂,你还没有'显示)。

您应该使用嵌套控制器技术shown in the documentation来访问包含的FXML文件的控制器。简而言之,在MainController你可以做到:

package controller;
public class MainController extends BaseController{

    // field names are formed by appending "Controller" to the fx:id attribute value:
    @FXML
    private ControlsController ControlsViewController;
    @FXML
    private ParametersController ParametersViewController;
    @FXML
    private CashRegisterController CashRegisterViewController;

    // Remove this constructor:
    // public MainController() {
        //controlsController = Controller.getInstance().getControlsController();
        //parametersController = Controller.getInstance().getParametersController();
        //cashRegisterController = Controller.getInstance().getCashRegisterController();
    // }

    public void initialize() {
        // give ControlsViewController access to CashRegisterViewController:
        ControlsViewController.setCashRegisterViewController(CashRegisterViewController);
    }
}

然后在ControlsController中定义明显的注入方法:

public class ControlsController {

    private CashRegisterController cashRegisterController ;

    public void setCashRegisterViewController(CashRegisterController cashRegisterController) {
        this.cashRegisterController = cashRegisterController ;
    }

    // existing code...

    private void StartSuperSim(){

        // getCashRegisterController().CreateCashRegisters();

        cashRegisterController.CreateCashRegisters();

        seconds = 0;

        setSuperSim(new Timeline(new KeyFrame(Duration.seconds((1 / controlsModel.getAcceleration())), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                seconds++;
                //System.out.println(seconds);

                if((seconds % (int)controlsModel.getParametersModel().getTimeBetweenCustomers()) == 0){
                    //System.out.println("Fired");
                }
            }
        })));
        getSuperSim().setCycleCount(Timeline.INDEFINITE);
        getSuperSim().play();
    }

    // ...
}

您定义的Controller单例类不会做任何有用的事情,因为它存储的控制器实例不是连接到相应FXML文件的实例。因此,这些控制器实例无法执行任何影响显示的UI的操作(例如,它们的@FXML - 注释字段都不会被初始化)。您应该删除该类及其所有引用。

其他一些注意事项:首先,控制器以你试图在这里做的方式互相访问并不是一个非常好的做法。使用MVC / MVP类型设计可能更好(你有视图和控制器,但在任何地方都没有模型)。然后,所有控制器只共享一个模型实例,并访问并观察其中的相同数据。有一个简单的例子,请查看Applying MVC With JavaFx

最后,请使用proper naming conventions。它使其他人更容易阅读和理解您的代码,语法高亮软件(例如本网站使用的软件)将更好地解析您的代码并正确突出显示它。