Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JavaFX NullPointerException when creating a new stage.

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JavaFX NullPointerException when creating a new stage.

    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.
    Last edited by Norm; August 29th, 2021 at 07:41 AM. Reason: Separated error message

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaFX NullPointerException when creating a new stage.

    because "this.pnClient" is null
    Where is pnClient given a value? It looks like the code is being executed with pnClient having a null value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JavaFX NullPointerException when creating a new stage.

    Hi Norm,

    Thank you for your reply. Yes you are right the GridPane this.pnClient is null.
    But is it possible to define and give values to Panes in JavaFX? If so, how would you go about this?

    Thanks in advance

    --- Update ---

    Hi Norm,
    I actually added the line:

    GridPane pnClient = new GridPane();

    Which seemed to have removed the NullPointerException error on pnClient, but I got the following new errors:

    Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    	at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1862)
    	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:49)
    	at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    	at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8889)
    	at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    	at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
    	at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    	at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    	at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    	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.reflect.InvocationTargetException
    	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)
    	... 44 more
    Caused by: 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:588)
    	... 55 more
    Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.text.Text.setText(String)" because "this.txtID" is null
    	at Login_App/LoginFX.MainFXMLController.initialize(MainFXMLController.java:478)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655)
    	... 58 more

    Now I'm getting this.txtID is null error, which seems to suggest I need to define all the scene variables?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JavaFX NullPointerException when creating a new stage.

    I don't do JavaFX coding and haven't seen many of its errors.
    If you can make a small, complete program that compiles and executes for testing, I can probably find the problem.
    The posted code is from a very large file that is to big to post here for testing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: August 4th, 2019, 09:30 AM
  2. JavaFX
    By bodylojohn in forum JavaFX
    Replies: 2
    Last Post: October 16th, 2017, 04:06 AM
  3. DB Connecting Refused for STAGE Environment.
    By rammohan0143 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 7th, 2013, 02:00 AM
  4. [SOLVED] JavaFx
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 28th, 2009, 09:13 PM

Tags for this Thread