I need to use a thread to update data on chart in my application continuously from controller class. The below code is the code of my controller class which is the readData method which is invoked by clicking the button in the GUI and the another method is the run method which update the chart.
@FXML
private LineChart<Number, Number> lineChart;
@FXML
void readData() {
Thread t = new Thread (new MainController());
t.start();
}
@Override
public void run() {
XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
while(true) {
series.getData().add(new XYChart.Data<Number, Number> (10,20));
series.getData().add(new XYChart.Data<Number, Number> (20,40));
series.getData().add(new XYChart.Data<Number, Number> (30,10));
series.getData().add(new XYChart.Data<Number, Number> (40,60));
lineChart.getData().add(series);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I got this error (Exception in thread "Thread-5" java.lang.NullPointerException
) on this line of code lineChart.getData().add(series);