JavaFx 中的 FXML 异常 |TableView - 表为 Null |Eclispe 集成开发环境

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

提问人:Sarmad Ali 提问时间:2/8/2023 最后编辑:James_DSarmad Ali 更新时间:2/8/2023 访问量:135

问:

在过去的 3-4 天里,我一直在研究它。我用谷歌搜索了一下,也看到了类似问题的答案,但它对我没有帮助。

Exception at Changing Scence:javafx.fxml.LoadException:

address-to-//application/Dashboard.fxml

这是我从登录页面登录仪表板时遇到的异常。通常,我不会收到此异常,但是当我尝试从xampp服务器在tableView中显示数据时,就会发生此异常,否则它工作正常。

这是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);
    
    }
}

这是登录按钮上的代码

/*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*/

这些是 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 java[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

更多说明

我有实现可初始化类的仪表板控制器.java。 在控制器类中,我有一个名为initialize的方法来实现。 这是这个方法

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

在这个方法中,我使用的是 loadData() 方法

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"));
    }

在loadData()方法中,我正在调用另一个方法,即

@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();
        }
         
        }

我认为问题出在某个地方,看到 StackTrace,因为它指出该表为 null,这就是所有问题发生的原因,但我不知道为什么该表为 null。

请帮我解决这个问题

javafx nullpointerexception xampp fxmlloader fileloadexception

评论

1赞 kleopatra 2/8/2023
请尽量减少可重现的示例 - 请注意 M,同时仍然可以按原样运行(没有数据库,只是硬编码一些数据),但使用 Application、fxml、控制器。也就是说:可能是字段名称(fxml 或控制器)中的拼写错误或被覆盖的字段。无关:公开显示 java 代码时坚持 java 命名约定!
1赞 kleopatra 2/8/2023
..并确保完整的堆栈跟踪格式化为代码(4 个前导空格......似乎在接近结尾的某个地方丢失了:)

答:

-1赞 Benson Kiprono 2/8/2023 #1

在设置表 ArrayList 之前,请检查 ArrayList 是否有一些数据。此外,将 studentTable.setItems 移到循环之外,使其如下所示:

            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);
}

评论

0赞 Sarmad Ali 2/8/2023
仍在获取 studentTable 的 NullPointerException
0赞 Sarmad Ali 2/8/2023
因此,这意味着 studentDataList 为 null 而不是 studentTable。现在,数据库或从数据库中检索数据是否有任何问题?
0赞 James_D 2/8/2023
这将有什么帮助?如果未正确注入原始代码,则也不会注入此代码。studentTable
1赞 James_D 2/8/2023
@SarmadAli “这意味着 studentDataList 是 null,而不是 studentTable”。哼?堆栈跟踪明确指出该值为 null。studentTable
-1赞 Sarmad Ali 2/8/2023 #2

感谢大家对我的问题感兴趣。

这是我解决它的方法。

我创建了一个单独的类来处理 tableView 的内容,并使该类成为 tableView 的 fxml 的控制器。 然后一切都很顺利:)

评论

1赞 kleopatra 2/8/2023
很高兴你解决了你的问题,但是..井。。事实上,无论是问答还是回答,对未来的读者都没有太大的帮助。