【问题标题】:Null pointer exception on passing a value between 2 controllers在 2 个控制器之间传递值时出现空指针异常
【发布时间】:2015-07-29 13:43:35
【问题描述】:

简介

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

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

这里的特别之处在于,我决定尝试对窗口进行编码的程序性和声明性。这意味着生成密码的窗口被编码在一个 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) displayPasswordController 和 newPswdController 一样吗? 2)你如何用@FXML注释NewPswdController newPswdController,newPswdController是嵌套控制器还是自定义组件? 3)IMO在这里你不需要使用绑定。您不会立即观察更改后的密码值,不是吗?弹出窗口后,用户将选择一些值,然后单击“完成”按钮,您将获得用户选择的值。您可以在 SO 中搜索“两个控制器之间的通信、数据传输、访问数据”以查看类似的用例。
  • @UlukBiy 1) 是的,它们是一样的,我编辑了我的问题(我猜我的名字选择很糟糕)。 2)这个控制器被注释为@FXML,因为我将它链接到我的 FXML 文件(对应于显示密码的视图),如下所示:fx:controller="my Controller" 3)不,当密码生成时会发生什么,我也加载和显示下一个视图(通过改变舞台的场景)。密码显示在 TextField 中。我将研究您的建议,谢谢。
  • @UlukBiy 非常感谢,您搜索其他用例以访问控制器之间的数据的建议起到了作用,因为我找到了一个完美运行且不难的解决方案。我找到了为什么我有一个空指针,因为如果一个控制器链接到 FXML 文件,你就不能以任何你想要的方式弄乱这个控制器,有一个特定的方法是使用 FXML 加载器获取控制器。跨度>
  • 是的,fx:controller 属性是 FXMLLoader 的指令,与@FXML 无关。很高兴得到帮助。您可以在您的解决方案中发布答案,并可能将您受益的问答链接作为信用。
  • 谢谢,我在那里学到了新东西!

标签: java binding javafx controller fxml


【解决方案1】:

问题出在显示密码的控制器上。空指针很可能是因为没有该控制器的实例。

问题是包含控制器的部分使用@FXML注释显示密码。

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

try {
  //Load the view and controller
  FXMLLoader loader = new FXMLLoader(getClass().getResource("displayPassword.fxml"));
  Parent displayPassword = (Parent)loader.load();
  Scene displayPasswordScene = new Scene(displayPassword);
  displayPasswordController controller = loader.getController();
  //Generate the password and set it
  controller.setPwdValue(Pastis.model.getNewPassword());
  //Load the new view on the stage
  Main.getStage().setScene(displayPasswordScene);
  Main.getStage().show();      
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

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

【讨论】:

    猜你喜欢
    • 2020-09-22
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多