Introduction
I am learning Java and JavaFX and in order to do that I am working on a little project which objective is to behave as password generator.
Basically, I have 2 windows, the first one allows the user to chose what kind of password he wants. Once he is done with that, the password is generated and it should be displayed on the second window.
What is particular here is that I decided to try out the procedural and declarative for coding the windows. Which means that the windows generating the password is coded in a java file. A contrario, the window that displays the password is declared in a FXML file.
What I struggle to do is to pass the generated password to the second window. I tried many things (bad things like using static methods) and I thought about trying to use bindings (that I only recently discovered).
But this last option didn't help either as I still get the same error all the time : a null pointer exception. It is coming from the line where the password is generated by the model and the obtained String is bound to a value in the controller of the seconde view.
I am kind of stuck here and I am thinking that mixing up 2 different ways to code my views isn't the best method. Still, maybe am I not doing the binding correctly, that's what I think and hope the most.
The code
So the controller of my first view looks like this (generating the password) :
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()));
}
}
And the controller of the view that displays the password :
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));
}
}
The null pointer exception appears at the specific line in the controller of the view that generates the password where the model actually generates it. I am giving it, I guess it should help the most, but I couldn't really use that information untill now:
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)
Thank you for looking into it. I might learn something new there I guess, but it seems that I can't find it out by myself and it is driving me nuts since a couple of days now.
If it appears that more information is needed, I can provide more. I included everything I think is necessary.