I’m working on my final programming task for school and wanted to add a piece of little functional eye candy to my java application, in the form of a dynamic Pie Chart in JavaFx.
Using Netbeans 7.2 rc2, I created a swing/javafx hybrid application from the templates. I extended the JPanelClass to create a shell with which my main application could use to access the FX features. The problem I’m facing, however is that while the initialisation of the chart goes smoothly, when I try to update the blasted thing, it generates a Null Pointer Exception.
Here I believe is the pertinent code:
Instantiation:
public class PieChartJFxPanel extends JApplet {
private static final int JFXPANEL_WIDTH_INT = 500;
private static final int JFXPANEL_HEIGHT_INT = 500;
StackPane root = new StackPane();
PieChart athleteChart;
ObservableList<PieChart.Data> pieChartData =FXCollections.observableArrayList();
private static JFXPanel fxContainer;
private int fAthletes=0;
private int rAthletes=0;
public PieChartJFxPanel(int totalAthletes,int finishedAthletes){
super();
fAthletes=finishedAthletes;
rAthletes=totalAthletes-fAthletes;
}
@Override
public void init() {
fxContainer = new JFXPanel();
fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
add(fxContainer, BorderLayout.CENTER);
// create JavaFX scene
Platform.runLater(new Runnable() {
@Override
public void run() {
createScene();
}
});
}
The Scene Constructor:
private void createScene() {
pieChartData.add(new PieChart.Data("Finished",fAthletes));
pieChartData.add(new PieChart.Data("Outstanding",rAthletes));
athleteChart=new PieChart();
athleteChart.setLegendVisible(false);
athleteChart.setTitle("Race Status");
//athleteChart.setAnimated(true);
athleteChart.setData(pieChartData);
root.getChildren().add(athleteChart);
fxContainer.setScene(new Scene(root));
//
}
And the method which generated the exception:
public void setData(int total, int in){
final ObservableList<PieChart.Data> pieChartDat=FXCollections.observableArrayList();
pieChartDat.add(new PieChart.Data("Finished",fAthletes));
pieChartDat.add(new PieChart.Data("Outstanding",rAthletes));
athleteChart.setData(pieChartDat);
}
And the exception:
Exception in thread "main" java.lang.NullPointerException
at piechartjpanel.PieChartJFxPanel.setData(PieChartJFxPanel.java:95)
at piechartjpanel.AthleteIOChart.setAthletes(AthleteIOChart.java:62)
at piechartjpanel.Test.setter(Test.java:34)
at piechartjpanel.Test.main(Test.java:48)
Any help is appreciated 🙂
Thanks in advance
I discovered that because JavaFX uses objects as a data model, whenever an object’s parameter is changed, the chart automatically upadates, thus in the example I posted above to change the values of the pieChart, all I needed was something similar to the following:
I found that Swing components are also in the habit of doing this, using objects as dynamic data types, automatically changing to reflect appearance to reflect changing values. I’m surprises no one spotted this, perhaps I worded my question badly.
Thanks All.