1

I want to call autoRefresh() method from Interface_adminController on interface_pasienController when I click the button. But there's an error "java.lang.NullPointerException". What's wrong with the code?

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

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

  • Also include the full stack trace and mark which line throws the exception. – c0der Aug 01 '19 at 09:26
  • If you know `minUpdateInterval` is never modified, `Timeline` may be a better alternative to `AnimationTimer`. You can find an example in this answer: https://stackoverflow.com/a/9966213/2991525 (This doesn't fix the issue described in the question though.) – fabian Aug 01 '19 at 12:47

1 Answers1

0

If you want to call the function from the Controller then you have to do

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

You first have to run loader.load() then you can get the Controller.

Raw
  • 461
  • 7
  • 15
  • It should be noted that there is no code actually adding the nodes loaded by `FXMLLoader` to a scene. If the scene displayed on screen is loaded in some other place, this code won't yield the desired result... – fabian Aug 01 '19 at 08:06
  • Yes, you are right. I just mean he has to call the `loader.load()` at first and then get the `Controller`. – Raw Aug 01 '19 at 08:52
  • This will most likely throw `java.lang.ClassCastException` – Mayur Patel Aug 01 '19 at 11:26