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

时间:2019-08-01 03:50:06

标签: java javafx

当我单击按钮时,我想从interface_pasienController上的Interface_adminController调用autoRefresh()方法。但是有一个错误“ java.lang.NullPointerException”。代码有什么问题?

interface_pasienController.java

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

Interface_adminController.java

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

线程“ JavaFX Application Thread”中的异常   java.lang.RuntimeException:   java.lang.reflect.InvocationTargetException引起原因:   java.lang.NullPointerException在   antrianpasienfarmasi.interface_pasienController.actionAmbilAntrian(interface_pasienController.java:101)     ... 58更多

1 个答案:

答案 0 :(得分:0)

如果要从Controller调用函数,则必须这样做

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

首先必须运行loader.load(),然后才能获取Controller。