-2

I have been working on it for the past 3-4 days. I have googled it and also saw similar questions' answers but it did not help me.

Exception at Changing Scence:javafx.fxml.LoadException:

address-to-//application/Dashboard.fxml

This is the Exception I am getting when I log into the dashboard from the login page. Usually, I am not getting this exception but when I am trying to display the data in tableView from the xampp server then this exception occurs otherwise it is working fine.

here is the code of Main.java

public class Main extends Application {
    
        private static Stage stg;

    @Override
    public void start(Stage primaryStage)  {
        
        stg = primaryStage;

        try {
            
        primaryStage.setResizable(false);
        Parent root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
        
        primaryStage.setTitle("My Application Title");
        primaryStage.setResizable(false);
        
        primaryStage.setScene(new Scene(root , 1200,670));
        primaryStage.show();
        
        }catch(Exception e) {
            
            System.out.println("Exception Occured: " +e);
        }
    }
    
    //to change the scene
    public void changeScene(String fxml) {
        //passing XML file via a string parameter
        try {
        Parent pane = FXMLLoader.load(getClass().getResource(fxml));
        stg.getScene().setRoot(pane);
        }catch(Exception e) {
            System.out.println("Exception at Changing Scence:" +e);
        }
    }
    
    public static void main(String[] args) {
        
        launch(args);
    
    }
}

and this is the code on the login button

/*TO CHECK THE LOGIN 
     * CREDENTIALS STARTS*/
    @FXML
    public void loginUser(ActionEvent event) {
        
        uname = username.getText().toString();
        upass = password.getText().toString();
        
         try {
            DbConnectivity db = new DbConnectivity(); 
            pst = db.connect.prepareStatement("SELECT * FROM adminlogin WHERE admin=? AND password=?");
            pst.setString(1, uname);
            pst.setString(2, upass);
            
            rst = pst.executeQuery();
            
        if(uname.isEmpty() && upass.isEmpty()) {
            
            wronginput.setText("Please Enter Credentials!");
         
        } else if(rst.next()) {
            
                wronginput.setText("Log In Success!");
                Main main = new Main();
                main.changeScene("/application/Dashboard.fxml");
                
            } else {
                
                wronginput.setText("Wrong Username or Password");
                username.setText("");
                password.setText("");
                username.requestFocus();
                
            }
            
            
        } catch (SQLException e) {
            
            System.out.println("Prepared Exception: "+e);
        }
         

    }
    /*TO CHECK LOGIN 
     * CREDENTIALS ENDS*/

And these are StackTrace

javafx.fxml.LoadException: 
address-to-/application/Dashboard.fxml

    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2714)
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2692)
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2555)
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3368)
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3324)
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3292)
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3264)
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3240)
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3233)
    at application.Main.changeScene(Main.java:39)
    at application.LoginControl.loginUser(LoginControl.java:59)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
    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:568)
    at javafx.base@19.0.2.1/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml@19.0.2.1/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
    at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base@19.0.2.1/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics@19.0.2.1/javafx.scene.Node.fireEvent(Node.java:8923)
    at javafx.controls@19.0.2.1/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls@19.0.2.1/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:207)
    at javafx.controls@19.0.2.1/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base@19.0.2.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base@19.0.2.1/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics@19.0.2.1/javafx.scene.Scene$MouseHandler.process(Scene.java:3894)
    at javafx.graphics@19.0.2.1/javafx.scene.Scene.processMouseEvent(Scene.java:1887)
    at javafx.graphics@19.0.2.1/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2620)
    at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
    at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
    at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
    at javafx.graphics@19.0.2.1/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
    at javafx.graphics@19.0.2.1/com.sun.glass.ui.View.notifyMouse(View.java:937)
    at javafx.graphics@19.0.2.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@19.0.2.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TableView.setItems(javafx.collections.ObservableList)" because "this.studentTable" is null
    at application.DashboardController.RefreshStudentRecord(DashboardController.java:343)
    at application.DashboardController.loadData(DashboardController.java:289)
    at application.DashboardController.initialize(DashboardController.java:281)
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2662)
    ... 66 more

For more clarifications

I have DashboardController.java that is implementing Initializable class. Inside the controller class I have method to implement called initialize. here is this method

@Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
        
        loadData();
        
    }

In this method I am using loadData() method that is

private void loadData() {
        DbConnectivity db = new DbConnectivity();
        conn = db.getConnection();
        
        RefreshStudentRecord();
        
        stsno.setCellValueFactory(new PropertyValueFactory<>("stsno"));
        stsname.setCellValueFactory(new PropertyValueFactory<>("stsname"));
        stsfname.setCellValueFactory(new PropertyValueFactory<>("stsfname"));
        stsid.setCellValueFactory(new PropertyValueFactory<>("stsid"));
        stscnic.setCellValueFactory(new PropertyValueFactory<>("stscnic"));
        stsphone.setCellValueFactory(new PropertyValueFactory<>("stsphone"));
        stsaddress.setCellValueFactory(new PropertyValueFactory<>("stsaddress"));
        stsclass.setCellValueFactory(new PropertyValueFactory<>("stsclass"));
    }

Inside the loadData() method I am calling another method that is

@FXML
    public void RefreshStudentRecord() {

         studentDataList.clear();
         studentquery = "SELECT * FROM studentdata";
         
         try {
            studentPst = conn.prepareStatement(studentquery);
            studentRst = studentPst.executeQuery();
            
            while(studentRst.next()) {
                studentDataList.add(new StudentsDataController(
                        studentRst.getInt("S_NO"),
                        studentRst.getString("studentname"),
                        studentRst.getString("fathername"),
                        studentRst.getString("studentid"),
                        studentRst.getString("studentcnic"),
                        studentRst.getString("phone"),
                        studentRst.getString("address"),
                        studentRst.getString("class")
                        )
                        );
                studentTable.setItems(studentDataList);
            }
            
        } catch (SQLException e) {
            System.out.println("Exception at Refresing student data: "+e);
            e.printStackTrace();
        }
         
        }

I think the problem is somewhere thereby, seeing the StackTrace as it is pointing out that the table is null that's why all of the problems have occurred, but I have no clue why the table is null.

Kindly help me to solve this problem

James_D
  • 201,275
  • 16
  • 291
  • 322
  • 1
    [mcve] please - mind the __M__ while still runnable as-is (no database, simply hard-code some data) but with Application, fxml, controller. That said: could be a typo in the field name (either fxml or controller) or an overwritten field. Unrelated: stick to java naming convention when showing java code publicly! – kleopatra Feb 08 '23 at 13:31
  • 1
    .. and make sure the complete stacktrace is formatted as code (4 leading spaces ... which seem to lost somewhere near the end :) – kleopatra Feb 08 '23 at 13:34

2 Answers2

-1

Before setting the table ArrayList, do a check to check if the ArrayList has some data. Also, move your studentTable.setItems outside the loop such that it looks as follows:

            while(studentRst.next()) {
            studentDataList.add(new StudentsDataController(
                    studentRst.getInt("S_NO"),
                    studentRst.getString("studentname"),
                    studentRst.getString("fathername"),
                    studentRst.getString("studentid"),
                    studentRst.getString("studentcnic"),
                    studentRst.getString("phone"),
                    studentRst.getString("address"),
                    studentRst.getString("class")
                    )
                    );
        }
if(studentDataList.size()>0){
            studentTable.setItems(studentDataList);
}
Benson Kiprono
  • 129
  • 1
  • 1
  • 12
  • Still getting the NullPointerException for studentTable – Sarmad Ali Feb 08 '23 at 09:55
  • So, it means that studentDataList is null not the studentTable. Now, is there any problem with the database or in retrieving the data from the database? – Sarmad Ali Feb 08 '23 at 10:43
  • How would this help? If `studentTable` is not correctly injected in the original code, it is not going to be injected in this code either. – James_D Feb 08 '23 at 13:59
  • 1
    @SarmadAli *"it means that studentDataList is null not the studentTable"*. Huh? Your stack trace clearly states that `studentTable` is null. – James_D Feb 08 '23 at 14:00
-1

Thanks to all for taking interest in my Question.

Here is how I solved it.

I created a separate class to handle the stuff for the tableView and made this class the controller of the fxml of tableView. Then everything went right :)

  • 1
    happy you fixed your problem, but .. well .. and as-are, neither question nor answer are not overly helpful to future readers. – kleopatra Feb 08 '23 at 14:48