我是javafx和scenebuilder的新手,但在学校的一个项目后感兴趣。
我正在尝试编写一个用户使用他们的帐户登录并进入主窗口的应用程序。
在主窗口中,我想要一个标签来显示该帐户所属的人(用于登录的用户名)。
我创建了一个上下文类,用于存储用于登录的用户名。
在主窗口的控制器类的start方法中,我尝试使用settext(Context.username)但最终得到nullpointerexception。当我打印出Context.username时,我得到正确的值,如果我将settext方法绑定到一个按钮,一切正常。
我的猜测是我在从FXML文件加载标签之前尝试使用settext。
我在网上看了好几个小时但找不到问题的解决方案。
请帮忙。
下面是调用第二个控制器类的方法。
@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());
}
}
下面是第二个控制器类(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)");
}
}
我也尝试在.init()方法中加载FXML文件,在main中加载settext但会出现同样的问题。
以下是来自controller2-class(main)的FXML文件的草稿。
<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>
下面是打印输出和堆栈跟踪。
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)
没有settext(),代码工作得很好;方法
如果您需要我的任何其他信息,请告知我们,我们将非常感谢您的帮助。