我正在尝试运行以下代码,它会在getSeriesVisible上抛出异常:
chart.addChangeListener(new ChartChangeListener() {
int indexChanged = -1;
@Override
public void chartChanged(ChartChangeEvent event) {
XYPlot ff = chart.getXYPlot();
XYItemRenderer y = ff.getRenderer();
boolean b = y.getSeriesVisible(0);
// chart.getXYPlot().getRenderer().setSeriesVisible(0, b);
}
});
消息:线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException
任何想法?
更新:我正在添加几个系列并生成图表如下:
XYSeriesCollection data = new XYSeriesCollection();
XYSeries series = new XYSeries("Series 1", true);
series.add(1, 2);
series.add(3, 5);
series.add(8, 10);
series.add(11, 3);
series.add(8, 10);
data.addSeries(series);
series = new XYSeries("Series 2");
series.add(5, -2);
series.add(7, 6);
series.add(8, 12);
series.add(11, -2);
series.add(15, 10);
data.addSeries(series);
final JFreeChart chart = ChartFactory.createXYLineChart("Chart", "X", "Y", data, PlotOrientation.VERTICAL, true, true, false);
答案 0 :(得分:1)
它必须位于代码中的其他位置。我在下面使用修改后的addButton()
监听器看到了此example的预期结果。
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int n = dataset.getSeriesCount();
dataset.addSeries("Series" + n, createSeries(n));
XYPlot plot = chart.getXYPlot();
XYItemRenderer renderer = plot.getRenderer();
System.out.println(renderer.isSeriesVisible(n));
}
});