gpt4 book ai didi

java - 在 2 个 Controller 之间传递值时出现空指针异常

转载 作者:行者123 更新时间:2023-11-30 08:11:47 24 4
gpt4 key购买 nike

简介

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

基本上,我有两个窗口,第一个窗口允许用户选择他想要的密码类型。一旦完成,密码就会生成,并且应该显示在第二个窗口上。

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

我努力做的是将生成的密码传递到第二个窗口。我尝试了很多事情(不好的事情,比如使用静态方法),并且我考虑尝试使用绑定(bind)(我最近才发现)。

但是最后一个选项也没有帮助,因为我仍然总是遇到相同的错误:空指针异常。它来自模型生成密码的行,并将获得的字符串绑定(bind)到第二 View Controller 中的值。

我有点被困在这里,我认为混合两种不同的方法来编码我的 View 并不是最好的方法。不过,也许我没有正确地进行绑定(bind),这是我最想和希望的。

<小时/>

代码

所以我的第一个 View 的 Controller 如下所示(生成密码):

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()));
}
}
<小时/>

以及显示密码的 View Controller :

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));
}
}
<小时/>

空指针异常出现在生成密码的 View Controller 中模型实际生成密码的特定行处。我正在提供它,我想它应该最有帮助,但直到现在我才能真正使用该信息:

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)

感谢您的调查。我想我可能会在那里学到一些新东西,但似乎我自己无法找到它,这几天以来它让我发疯。

如果需要更多信息,我可以提供更多信息。我包含了我认为必要的所有内容。

最佳答案

问题来自于显示密码的 Controller 。空指针很可能来自于没有该 Controller 的实例。

问题在于包含 Controller 的部分,该 Controller 使用@FXML注释显示密码。

以下代码在生成密码的 Controller 中运行良好:

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? .

关于java - 在 2 个 Controller 之间传递值时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30296668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com