2012-08-06 56 views
1

我試圖運行下面的代碼拋出異常,它拋出的異常getSeriesVisible:的JFreeChart上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); 

回答

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)); 
    } 
}); 
+0

謝謝,它被認爲是isSeriesVisible – Bober02 2012-08-07 06:18:36