Javafx 2 “线程中的异常”;JavaFX应用程序线程;java.lang.NullPointerException
我正在桌面上编写一个程序,它接受一个COM端口数组,并将其动态显示在图表上。程序运行一段时间没有出现故障,但随后出现以下异常:Javafx 2 “线程中的异常”;JavaFX应用程序线程;java.lang.NullPointerException,javafx-2,javafx-8,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.sc
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);
}
公共类FXMLController实现可初始化{
公共静态XYChart.Series hourDataSeries;
@FXML
公共标签;
@FXML
公共标签状态栏;
@FXML
公共线形图;
@FXML
公共号码xis xAxis;
@FXML
数字轴yAxis;
@FXML
菜单项关闭;
@FXML
菜单项选项;
@FXML
了解有关;
@FXML
按钮连接;
@FXML
公共标签字符串;
@FXML
私有void close(ActionEvent事件)抛出SerialPortException{
//COMConnect.serialPort.closePort();
系统出口(0);
}
@FXML
专用void connect(ActionEvent事件){
OpenScene OpenScene=newopenscene(“/SceneCOM.fxml”,“Options”);
}
@FXML
私有void显示统计信息(ActionEvent事件){
OpenScene OpenScene=newopenscene(“/SceneStatistics.fxml”,“Statistics”);
}
@FXML
私有void aboutit(ActionEvent事件){
OpenScene OpenScene=newopenscene(“/SceneAbout.fxml”,“About”);
}
@FXML
私有void getconnect(ActionEvent事件){
if(COMConnect.b==true){
COMConnect.b=错误;
试一试{
COMConnect.serialPort.closePort();
}捕获(SerialPortException例外){
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE,null,ex);
}
connect.setText(“connect”);
}否则{
COMConnect.b=真;
COMConnect.COMConnect();
}
}
@凌驾
公共void初始化(URL、ResourceBundle rb){
hourDataSeries=新的XYChart.Series();
label.textProperty().bind(COMConnect.EventListener.valueProperty);
COMConnect.connectProperty.setValue(“连接”);
connect.textProperty().bindInteractive(COMConnect.connectProperty);
statusbar.textProperty().bind(Typewriter.status);
hexString.textProperty().bind(TypewriterHexString.hexStr);
xAxis.setLabel(“时间”);
xAxis.settick单元(1);
setTickLabelFormatter(新NSC());
yAxis.setLabel(“密度,”+上标(“kg/m3”);
xAxis.setForceZeroInRange(假);
线形图。setLegendVisible(假);
lineChart.setCursor(Cursor.CROSSHAIR);
lineChart.getData().add(hourDataSeries);
}
}
并将数据从其他类添加到线形图
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++;
}
});
}
javafx.application.Platform.runLater(新的Runnable(){
@凌驾
公开募捐{
valueProperty.setValue(格式(第二)+上标(“kg/m3”);
如果(y>10){
hourDataSeries.getData().remove(0);
}
数据=新的XYChart.data(y,秒);
data.setNode(新的悬停ThresholdNode(0,第二个“”);
hourDataSeries.getData().add(数据);
System.out.println(hourDataSeries.getData().toString());
y++;
}
});
}
因此,解决方案非常简单:
lineChart.setAnimated(false);
该应用程序正常工作约25小时。
谢谢大家 共享一些代码,否则无法看到发生了什么on@Jos程序每22秒从COM端口接收一次数据。上次接收数据720次后出错。然后我得到了这个例外。恐怕这是JRE中的一个bug。您使用的是什么串行库?@JoséPereda jssc 2.8.0您可以在《JavaFX 8示例介绍》一书中找到一个从串行端口打印数据的示例。