线程“JavaFX Application Thread”中的异常java.lang.NullPointerException

时间:2014-12-17 07:51:14

标签: javafx-2 javafx-8

我正在桌面上编写一个程序,它接受一个COM端口阵列并动态地在图表上显示它。程序运行一段时间没有失败,但随后失败,出现以下异常:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
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:521)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:505)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$399(QuantumToolkit.java:334)
at com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$40/432581434.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$144(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)

应用程序继续运行。继续将数据保存到文本文件,但除关闭按钮外,窗口没有活动任何按钮。 如果有人有任何想法,请帮助。

这是一些代码

public class FXMLController implements Initializable {

public static XYChart.Series<Number, Number> hourDataSeries;

@FXML
public Label label;
@FXML
public Label statusbar;
@FXML
public LineChart lineChart;
@FXML
public NumberAxis xAxis;
@FXML
NumberAxis yAxis;
@FXML
MenuItem close;
@FXML
MenuItem Options;
@FXML
MenuItem about;
@FXML
Button connect;
@FXML
public Label hexString;

@FXML
private void close(ActionEvent event) throws SerialPortException {
    //COMConnect.serialPort.closePort();
    System.exit(0);
}

@FXML
private void connect(ActionEvent event) {
    OpenScene openScene = new OpenScene("/SceneCOM.fxml", "Options");
}

@FXML
private void showStatistics(ActionEvent event) {
    OpenScene openScene = new OpenScene("/SceneStatistics.fxml", "Statistics");
}

@FXML
private void aboutit(ActionEvent event) {
    OpenScene openScene = new OpenScene("/SceneAbout.fxml", "About");
}

@FXML
private void getconnect(ActionEvent event) {

    if (COMConnect.b == true) {
        COMConnect.b = false;
        try {
            COMConnect.serialPort.closePort();
        } catch (SerialPortException ex) {
            Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
        }
        connect.setText("Connect");
    } else {
        COMConnect.b = true;
        COMConnect.comConnect();

    }

}

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

    hourDataSeries = new XYChart.Series<Number, Number>();
    label.textProperty().bind(COMConnect.EventListener.valueProperty);
    COMConnect.connectProperty.setValue("Connect");
    connect.textProperty().bindBidirectional(COMConnect.connectProperty);
    statusbar.textProperty().bind(Typewriter.status);
    hexString.textProperty().bind(TypewriterHexString.hexStr);
    xAxis.setLabel("Time");
    xAxis.setTickUnit(1);
    xAxis.setTickLabelFormatter(new NSC());
    yAxis.setLabel("density," + superscript(" kg/m3"));

    xAxis.setForceZeroInRange(false);
    lineChart.setLegendVisible(false);
    lineChart.setCursor(Cursor.CROSSHAIR);
    lineChart.getData().add(hourDataSeries);

}

}

并从其他类

向LineChart添加数据
javafx.application.Platform.runLater(new Runnable() {
                        @Override
                        public void run() {

                            valueProperty.setValue(formatt(second) + superscript(" kg/m3"));

                            if (y > 10) {
                                hourDataSeries.getData().remove(0);

                            }
                            data = new XYChart.Data<Number, Number>(y, second);
                            data.setNode(new HoveredThresholdNode(0, second, ""));
                            hourDataSeries.getData().add(data);
                            System.out.println(hourDataSeries.getData().toString());
                            y++;
                        }
                    });
                }

1 个答案:

答案 0 :(得分:1)

因此,解决方案非常简单:

lineChart.setAnimated(false);

应用程序正常工作约25小时。 谢谢大家!