在两个控制器之间传递值时出现空指针异常

时间:2015-05-18 06:45:35

标签: java binding javafx controller fxml

简介

我正在学习Java和JavaFX,为了做到这一点,我正在开发一个项目,其目标是充当密码生成器。

基本上,我有2个窗口,第一个允许用户选择他想要的密码。完成后,密码将生成,并应显示在第二个窗口中。

这里特别的是我决定尝试编写windows的程序和声明。这意味着生成密码的窗口在java文件中编码。相反,显示密码的窗口在FXML文件中声明。

我很难做的是将生成的密码传递给第二个窗口。我尝试过很多东西(比如使用静态方法这样的坏事),我想过尝试使用绑定(我最近才发现)。

但是这最后一个选项也无济于事,因为我仍然一直得到相同的错误:空指针异常。它来自模型生成密码的行,并且获取的String绑定到seconde视图的控制器中的值。

我有点被困在这里,我认为混合两种不同的方式来编码我的观点并不是最好的方法。不过,也许我没有正确地进行约束,这就是我的想法和希望最多。

代码

所以我的第一个视图的控制器看起来像这样(生成密码):

public class GeneratePasswordController implements EventHandler<MouseEvent>{

  @FXML private displayPasswordController displayPasswordController;

  @Override
  public void handle(MouseEvent event) {
    //Doing some stuff that works, then generating the password and null pointer exception occurs here
    //The method getNewPassword() returns a String (the password).
    //The model is accessed statically (an instance has been created in the Application file (Main.java)).
    displayPasswordController.pwdValueProperty().bind(Bindings.createStringBinding(()
                                          -> Main.myModel.getNewPassword()));
  }
}

显示密码的视图的控制器

public class NewPswdController {
  @FXML private TextField displayPassword;

  private final StringProperty pwdValue = new SimpleStringProperty("Password");

  public StringProperty pwdValueProperty() {
    return pwdValue;
  }

  public String getPwdValue() {
    return pwdValue.get();
  }

  public void setPwdValue(String value) {
    this.pwdValue.set(value);
  }

  @FXML
  void initialize() {
    dispPassword.textProperty().bind(Bindings.format("%s", pwdValue));
  }
}

空指针异常出现在视图控制器中的特定行,该行生成模型实际生成密码的视图。 我给它,我想它应该是最有帮助的,但我现在还不能真正使用这些信息:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at s03.GeneratePasswordController.handle(GeneratePasswordController.java:61)
at s03.GeneratePasswordController.handle(GeneratePasswordController.java:1)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$271/1952832519.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1232367853.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

感谢您对此进行调查。我可能会在那里学到一些新东西,但似乎我自己找不到它,这让我疯了几天。

如果看起来需要更多信息,我可以提供更多信息。我包括了我认为必要的一切。

1 个答案:

答案 0 :(得分:1)

麻烦来自显示密码的控制器。空指针很可能来自于没有该控制器的实例。

有什么问题包含控制器的部分,使用@FXML注释显示密码

以下代码在生成密码的控制器中运行正常:

{{ }}

我在这个问题上找到了答案:FXMLLoader getController returns NULL?