你好。我有点困了
我遇到一个错误,无法关闭以前的窗口来设置新窗口
这是我的控制器:
public class Controller implements Initializable {
//------------- init Order -------------//
@FXML
private Label text10;
@FXML
private Label text20;
@FXML
private Label text30;
@FXML
private Label text40;
@FXML
private Label text50;
@FXML
private TextField entry10;
@FXML
private TextField entry20;
@FXML
private TextField entry30;
@FXML
private Button btnConnect10;
//------------- init Reception -------------//
@FXML
private Label text11;
@FXML
private Label text21;
@FXML
private Label text31;
@FXML
private Label text41;
@FXML
private Label text51;
@FXML
private TextField entry11;
@FXML
private TextField entry21;
@FXML
private TextField entry31;
@FXML
private Button btnConnect11;
//------------- init Connexion -------------//
@FXML
private ImageView img;
@FXML
private Label user;
@FXML
private Label password;
@FXML
private TextField usr;
@FXML
private PasswordField passwd;
@FXML
private Button btnConnect13;
//------------- init popup -------------//
@FXML
private Label popup;
//------------- Stage -------------//
Stage prevStage;
public void setPrevStage(Stage stage) {
this.prevStage = stage;
}
@FXML
private void buttonPressed(KeyEvent e) {
if (e.getCode() == KeyCode.ENTER) {
System.out.println("je suis un krab");
}
}
@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
//init classe
Controller ctr = new Controller();
//fenetre Commande
if (event.getSource() == btnConnect10) {
ctr.Commande(entry10.getText(), entry20.getText(), entry30.getText());
}
//fenetre Reception
if (event.getSource() == btnConnect11) {
ctr.Reception(entry11.getText(), entry21.getText(), entry31.getText());
}
//fenetre connexion
if (event.getSource() == btnConnect13) {
ctr.Connect(usr.getText(), passwd.getText());
}
}
//Menu d'affchage
private void menu(int access) throws IOException {
//init fenetre
Stage stage = new Stage();
Pane myPane = null;
Scene scene;
switch (access) {
case 1:
stage.setTitle("Commande OR tool");
myPane = FXMLLoader.load(getClass().getResource("CommandeFXML.fxml"));
scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
stage.show();
break;
case 2:
//recupère les infos de la fenetre
stage.setTitle("Réception OR tool");
myPane = FXMLLoader.load(getClass().getResource("ReceptionFXML.fxml"));
scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
stage.show();
break;
case 3:
stage.setTitle("Admin OR tool");
myPane = FXMLLoader.load(getClass().getResource("AdminFXML.fxml"));
scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
stage.show();
break;
default:
//error
System.out.println("erreur changing window");
}
}private void Connect(String getuser, String getpasswd) throws IOException {
ConnectBDD bdd = new ConnectBDD();
Controller ctr = new Controller();
System.out.println("user = "+getuser+"npassword ="+getpasswd);
//test
if (!getuser.isEmpty() && !getpasswd.isEmpty()) {
//check if user exist
if (bdd.connectUserTest(getuser, getpasswd) == true) {
//get the access level
int acces = bdd.connectUser (getuser, getpasswd);
//Changing window
ctr.menu(acces);
}
else {
System.out.println("user not in the data base");
}
} //message d'erreur
//to do faire un pop up
else {
System.out.println("no entry");
}
}
以及主要的
public class JavaFX extends Application {
@FXML
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("OR tool");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("ConnectFXML.fxml"));
Pane myPane = (Pane) myLoader.load();
Controller controller = (Controller) myLoader.getController();
controller.setPrevStage(primaryStage);
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
我有一个很好的堆栈
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
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:8411)
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:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
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)
... 48 more
最重要的部分是
Caused by: java.lang.NullPointerException
at javafx.Controller.menu(Controller.java:149)
at javafx.Controller.Connect(Controller.java:303)
at javafx.Controller.handleButtonAction(Controller.java:130)
aligne 149是当我试图关闭以前的窗口时。控制器菜单功能中的"prevStage.close();"
ligne 303是连接测试中功能菜单的调用
当我调用连接函数时,ligne 130在handleButtonAction中当我尝试在处理程序中打开一个新窗口时,它可以完美地工作。但是我需要将窗口调整为用户的访问级别
但现在我得到了java.lang.NullPointerException。
@FXML
private void handleButtonAction(ActionEvent event) throws IOException {//init fenetre
Stage stage = new Stage();
Pane myPane = null;
Scene scene;stage.setTitle("Réception OR tool");
myPane = FXMLLoader.load(getClass().getResource("ReceptionFXML.fxml"));
scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
stage.show(); }
您正在代码中重复创建新的控制器实例:
Controller ctr = new Controller();
对于这些新实例,将不会初始化任何内容:您永远不会在这些实例上调用setPrevStage()
,因此prevStage
将不会被初始化,并且由于这些实例不是由FXMLLoader
创建的,所以@FXML
注释字段将不会初始化。
删除对Controller
构造函数的调用,并调用当前实例上的方法:
@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
// remove this:
//Controller ctr = new Controller();
//fenetre Commande
if (event.getSource() == btnConnect10) {
// replace this
// ctr.Commande(entry10.getText(), entry20.getText(), entry30.getText());
// with this
Commande(entry10.getText(), entry20.getText(), entry30.getText());
// or equivalently
// this.Commande(entry10.getText(), entry20.getText(), entry30.getText());
}
//fenetre Reception
if (event.getSource() == btnConnect11) {
Reception(entry11.getText(), entry21.getText(), entry31.getText());
}
//fenetre connexion
if (event.getSource() == btnConnect13) {
Connect(usr.getText(), passwd.getText());
}
}
在整个代码中也是如此。