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