I am new to javafx and scenebuilder but got interested after a project in school.
I am trying to write an application where the user login with their account and gets to the main window.
In the main window I want a label to show who the account belongs to (username used to login).
I have created a context class where I store the username used to login.
In the start method in the controller class for the main window I try use settext(Context.username) but end up with a nullpointerexception. When I print out Context.username I get the correct value and if I bind the settext method to a button everything works just fine.
My guess is that I try to use the settext before the label is loaded from the FXML-file.
I've looked around online for hours but can't find a solution to my problem.
Please help.
Below is the method calling the second controller-class.
@FXML
private TextField userName_txt;
@FXML
private PasswordField password_txt;
@FXML
public void checkLogin(ActionEvent event){
this.username = userName_txt.getText();
this.password = password_txt.getText();
Context.username = username;
Context.password = password;
try {
System.out.println("running check login with: " + username + " " + password);
if(dbq.checkLogin(username, password)) {
System.out.println("Successful login");
loginMessage_txt.setText("Successful login!");
loginMessage_txt.setFill(Color.GREEN);
Controller2 controller2 = new Controller2();
controller2.start(stage);
}else {
System.out.println("Wrong password or username");
loginMessage_txt.setText("Wrong password or username!");
loginMessage_txt.setFill(Color.RED);
}
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
Below is the start method for the second controller class (main).
public class Controller2 extends Application{
private DatabaseQueries dbq = new DatabaseQueries();
@FXML
private Label accountBelongsTo_lbl;
@Override
public void start(Stage mainStage) {
System.out.println("Running Controller2.start");
try {
Database db = new Database();
db.openConnection("root", "xxxxx");
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("main.fxml"));
Context.root = loader.load();
// root2.getChildrenUnmodifiable();
Scene scene = new Scene(Context.root);
System.out.println("Pre new scene: " + Context.stage.toString());
Context.stage.setScene(scene);
Context.stage.show();
Context.stage.centerOnScreen();
System.out.println("Post new scene: " + Context.stage.toString());
System.out.println("username is: " + Context.username);
accountBelongsTo_lbl.setText("Context.username");
}catch(Exception e) {
System.out.print("Exception in start (startmainstage)");
}
}
I've also tried loading the FXML-file in .init() method and the settext in main but the same problem occur.
Below is a draft from the FXML-file for the controller2-class (main).
<AnchorPane fx:id="ap_main" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="1000.0" style="-fx-background-color: White;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller2">
<Label fx:id="accountBelongsTo_lbl" layoutX="952.0" layoutY="19.0" text="Label" AnchorPane.rightAnchor="30.0">
<font>
<Font size="24.0" />
</font>
</Label>
Below is the print-outs and stack trace.
Connecting to database...
Connected to database!
Pre new scene: javafx.stage.Stage@725707ff
post new scene: javafx.stage.Stage@725707ff
running check login with: xxxxx 123456
Connecting to database...
Connected to database!
Successful login
Database connection closed.
Running Controller2.start
Connecting to database...
Connected to database!
Pre new scene: javafx.stage.Stage@725707ff
Post new scene: javafx.stage.Stage@725707ff
java.lang.NullPointerExceptionusername is: xxxxx
at application.Controller2.start(Controller2.java:146)
at application.Controller.checkLogin(Controller.java:81)
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)
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:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
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$MouseHandler.process(Scene.java:3757)
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:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
Code works just fine without settext(); method.
Please let me know if you need anything else from me, any help will be deeply appreciated.