我是JavaFX的初学者,希望能对您有所帮助。 我正在创建GUI,该GUI具有一些按钮和带有日期和时间的标签,并且除一个例外外,其他所有功能均正常运行。当我尝试通过按钮打开新窗口时,出现一些错误NullPointerException。新窗口显示正确,但错误每隔1秒钟定期刷新一次,如日期和时间标签。日期和时间始终在GUI中正确显示。我知道问题出在哪里,但我不知道如何解决。
我有Main.java其中和FXMLDocumentController.java。 GUI.fxml包含在FXMLDocumentController中声明的Panes和Label。
public class FXMLDocumentController implements Initializable {
@FXML
public Pane close_button;
@FXML
public Pane MainPane;
@FXML
public Label dateTime;
@FXML
private void close_button(MouseEvent event) {
Parent Close_window;
try {
Close_window = FXMLLoader.load(getClass().getResource("close_window.fxml"));
Stage close_window = new Stage();
close_window.setTitle("Potwierdzenie zamknięcia");
close_window.setScene(new Scene(Close_window, 400, 100));
close_window.show();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
private void close_confirm(MouseEvent event) {
Platform.exit();
}
@FXML
private void close_cancel(MouseEvent event) {
final Node close_window = (Node) event.getSource();
final Stage Close_window = (Stage) close_window.getScene().getWindow();
Close_window.close();
}
@FXML
private void initClock() {
Timeline clock = new Timeline(new KeyFrame(Duration.seconds(0), e -> {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
dateTime.setText(LocalDateTime.now().format(formatter));
}), new KeyFrame(Duration.seconds(1)));
clock.setCycleCount(Animation.INDEFINITE);
clock.play();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
initClock();
}
Main.java
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void init(){
System.out.println("Program opened");
}
@Override
public void start(Stage primaryStage) throws Exception{
/*************OPENING MAIN WINDOW*****************/
Parent login_window = FXMLLoader.load(getClass().getResource("GUI.fxml"));
primaryStage.setTitle("Panel diagnostyczny");
primaryStage.resizableProperty().setValue(Boolean.FALSE);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setScene(new Scene(login_window, 800, 600));
primaryStage.show();
}
@Override
public void stop()
{
System.out.println("Program stopped");
}
错误
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at sample.FXMLDocumentController.lambda$initClock$0(FXMLDocumentController.java:136)
at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:226)
at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:167)
at javafx.animation.Timeline.impl_playTo(Timeline.java:176)
at javafx.animation.AnimationAccessorImpl.playTo(AnimationAccessorImpl.java:39)
at com.sun.scenario.animation.shared.InfiniteClipEnvelope.timePulse(InfiniteClipEnvelope.java:126)
at javafx.animation.Animation.impl_timePulse(Animation.java:1102)
at javafx.animation.Animation$1.lambda$timePulse$24(Animation.java:186)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.animation.Animation$1.timePulse(Animation.java:185)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:344)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:506)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$346(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$450(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)
FXMLDocumentController的136行是dateTime.setText(LocalDateTime.now()。format(formatter));可能有问题,但我不知道有什么解决方案。