有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JavaFx通过另一个控制器调用一个方法

单击按钮时,我想从Interface\u PascienceController上的Interface\u adminController调用autoRefresh()方法。但是有一个错误“java.lang.NullPointerException”。代码怎么了

接口\u PascienceController。爪哇

@Override
   public void initialize(URL url, ResourceBundle rb) {

   }

@FXML
private int actionAmbilAntrian(ActionEvent event) throws SQLException, DocumentException, FileNotFoundException, IOException {
        System.out.println("You clicked me!");

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        String tanggal = dateFormat.format(date);

        DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        Date time = new Date();
        String waktu = timeFormat.format(time);

        System.out.println(dateFormat.format(date));
        System.out.println(timeFormat.format(time));

        String sql = "INSERT INTO antrian VALUES(?,?,?,?,?,?)";

        int id = autonumber();
        try (Connection conn = connection.connection();
             PreparedStatement pstmt = conn.prepareStatement(sql)
            ){                                  
                pstmt.setInt(1, id);
                pstmt.setString(2, tanggal);
                pstmt.setInt(3, id);
                pstmt.setString(4, waktu);
                pstmt.setString(5, "");
                pstmt.setBoolean(6, false);
                pstmt.executeUpdate();

                System.out.println("Insert to Database");

                System.out.println("Create PDF File");
                String filename = id + ".pdf";

                Document document = new Document();
                PdfWriter.getInstance(document, new FileOutputStream("D://"+id+".pdf"));
                document.open();
                if(id < 10){
                    document.add(new Paragraph("00"+ id));
                } else if(id > 9 && id < 99){
                    document.add(new Paragraph("0"+ id));
                } else if(id > 99){
                    document.add(new Paragraph(id));
                }
                document.add(new Paragraph("Tanggal: " + tanggal));
                document.add(new Paragraph("Waktu: " + waktu));
                document.close();

                FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/interface_admin.fxml"));
                Interface_adminController adminC = loader.getController();
                System.out.println("adminC = "+adminC);
                adminC.autoRefresh(); // an error: java.lang.NullPointerException


                System.out.println("itext PDF program executed");
            } catch(SQLException e){
                System.out.println(e);
                System.out.println("Failed");
            }
        return id;
}

与管理员控制器的接口。爪哇

@Override
    public void initialize(URL url, ResourceBundle rb) {
        showItem();
    }

    void autoRefresh() {
        final LongProperty lastUpdate = new SimpleLongProperty();
        final long minUpdateInterval = 0 ;

        AnimationTimer timer = new AnimationTimer() {

            @Override
            public void handle(long now) {
                try{
                    if (now - lastUpdate.get() > minUpdateInterval) {
                        showItem();
                        lastUpdate.set(now);
                    }
                } catch(Exception e){
                    System.out.println(e);
                }
            }
        };
        timer.start();
    }

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException Caused by: java.lang.NullPointerException at antrianpasienfarmasi.interface_pasienController.actionAmbilAntrian(interface_pasienController.java:101) ... 58 more


共 (1) 个答案

  1. # 1 楼答案

    如果要从Controller调用函数,则必须执行以下操作

    loader.load();
    Interface_adminController adminC = loader.getController();
    

    首先必须运行loader.load(),然后才能获得控制器