FXML Exception in JavaFx | TableView - Table is Null | Eclispe IDE

186 Views Asked by At

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 [email protected]/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2714)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2692)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2555)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3368)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3324)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3292)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3264)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3240)
    at [email protected]/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 [email protected]/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
    at [email protected]/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
    at [email protected]/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
    at [email protected]/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
    at [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at [email protected]/javafx.event.Event.fireEvent(Event.java:198)
    at [email protected]/javafx.scene.Node.fireEvent(Node.java:8923)
    at [email protected]/javafx.scene.control.Button.fire(Button.java:203)
    at [email protected]/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:207)
    at [email protected]/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at [email protected]/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    at [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at [email protected]/javafx.event.Event.fireEvent(Event.java:198)
    at [email protected]/javafx.scene.Scene$MouseHandler.process(Scene.java:3894)
    at [email protected]/javafx.scene.Scene.processMouseEvent(Scene.java:1887)
    at [email protected]/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2620)
    at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
    at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
    at [email protected]/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
    at [email protected]/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
    at [email protected]/com.sun.glass.ui.View.notifyMouse(View.java:937)
    at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at [email protected]/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 [email protected]/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

2

There are 2 best solutions below

4
Benson Kiprono On

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);
}
1
Sarmad Ali On

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 :)