Hi guys,
I'm fairly new to JavaFX and I've been trying to create multiple stages in my project.
So what I currently have is a main java class to open a Login Page under the Start Method, a Login Controller to open a Main Page and a Main Controller to open a new stage file called "Quotation FXML.fxml".
The Login page and the main page opens fine, but when I tried to open a Quotation FXML.fxml stage using a button, it gives me a NPE error.
//Initialise controller @Override public void initialize(URL url, ResourceBundle rb) { pnClient.toFront(); txtID.setText("ID"); txtName.setText("Clients"); txtMenuName.setText("/home/clients"); txtClientName.setText("Client Name"); txtLoc.setText("Location"); txtWS.setText("W/S Fee"); txtMat.setText("Material"); txtMD.setText("MD/SP Fee"); txtfEle.setVisible(false); txtfFee.setVisible(false); paneTitle.setBackground(new Background(new BackgroundFill(Color.rgb(229, 8, 8), CornerRadii.EMPTY, Insets.EMPTY ))); try { UpdateTable(); searchClients(); UpdateElement(); searchElements(); } catch (SQLException ex) { } } //adding pane title and colour to categories @FXML public void handleClicks (ActionEvent event) throws SQLException { if (event.getSource() == btnNew) { pnClient.toFront(); txtID.setText("ID"); txtName.setText("Clients"); txtMenuName.setText("/home/clients"); txtClientName.setText("Client Name"); txtLoc.setText("Location"); txtWS.setText("W/S Fee"); txtMat.setText("Material"); txtMD.setText("MD/SP Fee"); txtfLoc.setVisible(true); txtfWS.setVisible(true); txtfMat.setVisible(true); txtfMD.setVisible(true); txtLoc.setVisible(true); txtWS.setVisible(true); txtMat.setVisible(true); txtMD.setVisible(true); txtfID.setVisible(true); txtfClientName.setVisible(true); txtfEle.setVisible(false); txtfFee.setVisible(false); paneTitle.setBackground(new Background(new BackgroundFill(Color.rgb(229, 8, 8), CornerRadii.EMPTY, Insets.EMPTY ))); } else if (event.getSource() == btnMat) { txtName.setText("Material Detail"); txtMenuName.setText("/home/materials"); paneTitle.setBackground(new Background(new BackgroundFill(Color.rgb(0, 150, 209), CornerRadii.EMPTY, Insets.EMPTY ))); } else if (event.getSource() == btnSavedQ) { pnSavedQ.toFront(); txtName.setText("Saved Quotation"); txtMenuName.setText("/home/saved"); paneTitle.setBackground(new Background(new BackgroundFill(Color.rgb(255, 131, 0), CornerRadii.EMPTY, Insets.EMPTY ))); } else if (event.getSource() == btnPrintQ) { txtName.setText("Print Quotation"); txtMenuName.setText("/home/print"); paneTitle.setBackground(new Background(new BackgroundFill(Color.rgb(170, 65, 246), CornerRadii.EMPTY, Insets.EMPTY ))); } else if (event.getSource() == btnAss) { pnAss.toFront(); txtName.setText("Assay Detail"); txtMenuName.setText("/home/assay elements"); txtID.setText("Fee"); txtClientName.setText("Material Name"); txtfLoc.setVisible(false); txtfWS.setVisible(false); txtfMat.setVisible(false); txtfMD.setVisible(false); txtLoc.setVisible(false); txtWS.setVisible(false); txtMat.setVisible(false); txtfID.setVisible(false); txtfClientName.setVisible(false); txtMD.setVisible(false); txtfEle.setVisible(true); txtfFee.setVisible(true); paneTitle.setBackground(new Background(new BackgroundFill(Color.rgb(48, 168, 124), CornerRadii.EMPTY, Insets.EMPTY ))); } } @FXML public void setID (ActionEvent event) { String ID = txtfClientName.getText(); ID = ID.replaceAll("\\s+",""); ID = ID.toUpperCase(); ID = ID.substring(0,4); ID = ID+"01"+"KR"; txtfID.setText(ID); } public void createQuotationForm(ActionEvent event) throws IOException { FXMLLoader main = new FXMLLoader(); main.setLocation(getClass().getResource("/LoginFX/QuotationFXML.fxml")); Parent mainParent = main.load(); Scene mainScene = new Scene(mainParent); Scene currentScene = mainParent.getScene(); Stage stage = (Stage) currentScene.getWindow(); stage.setScene(mainScene); }javafx.fxml.LoadException: /Users/seokjuhan/NetBeansProjects/Login_App/build/classes/LoginFX/QuotationFXML.fxml at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2517) at Login_App/LoginFX.MainFXMLController.createQuotationForm(MainFXMLController.java:571) at Login_App/LoginFX.MainFXMLController.doubleclick(MainFXMLController.java:562) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76) at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273) at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83) at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857) at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1729) at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.base/javafx.event.Event.fireEvent(Event.java:198) at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3856) at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1851) at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2584) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447) at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446) at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556) at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942) at javafx.graphics/com.sun.glass.ui.mac.MacView.notifyMouse(MacView.java:127) Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.layout.GridPane.toFront()" because "this.pnClient" is null at Login_App/LoginFX.MainFXMLController.initialize(MainFXMLController.java:451) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655) ... 41 more
So this is the part of the code I have on my Main Controller. I think I understand why I'm getting NPE, it's because I'm trying to load QuotationFXML which doesn't have the elements such as pnClient, txtID and etc., that I initialised. So I thought maybe I need to have this createQuotationForm() code in a new Controller. My problem though is that, I have the button, which should opens the QuotationFXML file, on my MainFXML.fxml so I need to have the method createQuotationForm(ActionEvent event), in my Main Controller. I'm just out of ideas how to get around this problem.
Do I need to get rid of all the initialisers?
Any help would be great.
Thank you.