netbeans - Error switching screen Java Fx FXML -
hello. i'm kinda stuck.
got error, i'm not able close previous window set new one.
here controller :
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 access level int acces = bdd.connectuser (getuser, getpasswd); //changing window ctr.menu(acces); } else { system.out.println("user not in data base"); } } //message d'erreur //to faire un pop else { system.out.println("no entry"); } }
and main
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 command line arguments */ public static void main(string[] args) { launch(args); }
i got nice stack
exception in thread "javafx application thread" java.lang.runtimeexception: java.lang.reflect.invocationtargetexception @ javafx.fxml.fxmlloader$methodhandler.invoke(fxmlloader.java:1774) @ javafx.fxml.fxmlloader$controllermethodeventhandler.handle(fxmlloader.java:1657) @ com.sun.javafx.event.compositeeventhandler.dispatchbubblingevent(compositeeventhandler.java:86) @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:238) @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:191) @ com.sun.javafx.event.compositeeventdispatcher.dispatchbubblingevent(compositeeventdispatcher.java:59) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:58) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114) @ com.sun.javafx.event.eventutil.fireeventimpl(eventutil.java:74) @ com.sun.javafx.event.eventutil.fireevent(eventutil.java:49) @ javafx.event.event.fireevent(event.java:198) @ javafx.scene.node.fireevent(node.java:8411) @ javafx.scene.control.button.fire(button.java:185) @ com.sun.javafx.scene.control.behavior.buttonbehavior.mousereleased(buttonbehavior.java:182) @ com.sun.javafx.scene.control.skin.behaviorskinbase$1.handle(behaviorskinbase.java:96) @ com.sun.javafx.scene.control.skin.behaviorskinbase$1.handle(behaviorskinbase.java:89) @ com.sun.javafx.event.compositeeventhandler$normaleventhandlerrecord.handlebubblingevent(compositeeventhandler.java:218) @ com.sun.javafx.event.compositeeventhandler.dispatchbubblingevent(compositeeventhandler.java:80) @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:238) @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:191) @ com.sun.javafx.event.compositeeventdispatcher.dispatchbubblingevent(compositeeventdispatcher.java:59) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:58) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114) @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56) @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114) @ com.sun.javafx.event.eventutil.fireeventimpl(eventutil.java:74) @ com.sun.javafx.event.eventutil.fireevent(eventutil.java:54) @ javafx.event.event.fireevent(event.java:198) @ javafx.scene.scene$mousehandler.process(scene.java:3757) @ javafx.scene.scene$mousehandler.access$1500(scene.java:3485) @ javafx.scene.scene.impl_processmouseevent(scene.java:1762) @ javafx.scene.scene$scenepeerlistener.mouseevent(scene.java:2494) @ com.sun.javafx.tk.quantum.glassvieweventhandler$mouseeventnotification.run(glassvieweventhandler.java:380) @ com.sun.javafx.tk.quantum.glassvieweventhandler$mouseeventnotification.run(glassvieweventhandler.java:294) @ java.security.accesscontroller.doprivileged(native method) @ com.sun.javafx.tk.quantum.glassvieweventhandler.lambda$handlemouseevent$354(glassvieweventhandler.java:416) @ com.sun.javafx.tk.quantum.quantumtoolkit.runwithoutrenderlock(quantumtoolkit.java:389) @ com.sun.javafx.tk.quantum.glassvieweventhandler.handlemouseevent(glassvieweventhandler.java:415) @ com.sun.glass.ui.view.handlemouseevent(view.java:555) @ com.sun.glass.ui.view.notifymouse(view.java:937) @ com.sun.glass.ui.win.winapplication._runloop(native method) @ com.sun.glass.ui.win.winapplication.lambda$null$148(winapplication.java:191) @ java.lang.thread.run(thread.java:745) caused by: java.lang.reflect.invocationtargetexception @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ sun.reflect.misc.trampoline.invoke(methodutil.java:71) @ sun.reflect.generatedmethodaccessor1.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ sun.reflect.misc.methodutil.invoke(methodutil.java:275) @ javafx.fxml.fxmlloader$methodhandler.invoke(fxmlloader.java:1769) ... 48 more
the important part
caused by: java.lang.nullpointerexception @ javafx.controller.menu(controller.java:149) @ javafx.controller.connect(controller.java:303) @ javafx.controller.handlebuttonaction(controller.java:130)
ligne 149 when try close previous windows. "prevstage.close();" in menu function of controller
ligne 303 call of fuction menu in test of connexion.
ligne 130 in handlebuttonaction when call connect functionwhen try open new window in handler works perfectly. need adpat window access level of user.
i've got 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(); }
you repeatedly creating new controller instances in code:
controller ctr = new controller();
for these new instances, nothing initialized: never call setprevstage()
on these instances prevstage
not initialized and, since these instances not created fxmlloader
, @fxml
-annotated fields not initialized.
remove calls controller
constructor, , invoke methods on current instance:
@fxml private void handlebuttonaction(actionevent event) throws ioexception { // remove this: //controller ctr = new controller(); //fenetre commande if (event.getsource() == btnconnect10) { // replace // ctr.commande(entry10.gettext(), entry20.gettext(), entry30.gettext()); // 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()); } }
and throughout code.
Comments
Post a Comment