1

我正在尝试运行以下代码,它在 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);
4

1 回答 1

1

它必须在您的代码中的其他位置。我使用下面的修改后的侦听器看到了此示例的预期结果。addButton()

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));
    }
});
于 2012-08-06T21:13:11.853 回答