0

我正在使用 JFreechart 编写甘特图 - chartPanel 已创建并添加到现有的 JPanel。但是,在 JPanel validate() 方法之后,我在 GradientBarPainter.splitHorizo​​ntalBar() 中看到 NullPointerException

例外是在帖子的末尾。知道如何解决这个错误吗?

--------------------------
 private JFreeChart createChart(final IntervalCategoryDataset dataset) {
 final JFreeChart chart = ChartFactory.createGanttChart(
 "Gantt Chart - ESP Job representation ", // chart title
 "ESP Job name", // domain axis label
 "Time", // range axis label
 dataset, // data
 true, // include legend
 true, // tooltips
 false // urls
 ); 

CategoryPlot plot = (CategoryPlot) chart.getPlot();
 CategoryItemRenderer renderer = plot.getRenderer();

 renderer.setItemLabelGenerator(new IntervalCategoryItemLabelGenerator("{1}-{2}-{3}", new SimpleDateFormat(
 "HH:mm:ss")));

 renderer.setBaseItemLabelsVisible(true);
 renderer.setBaseItemLabelPaint(Color.BLACK);
 renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
 ItemLabelAnchor.INSIDE6, TextAnchor.CENTER));

 return chart; 
}
.....

 JFreeChart chart = createChart(collection);
 ChartPanel chartPanel = new ChartPanel(chart, false);
 chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
 chartPanel.setDomainZoomable(true);
 rightPanel.add(chartPanel, BorderLayout.CENTER);
 rightPanel.validate();


-------------------------------------

JPanel“rightPanel”是使用 Netbeans 添加的,并且布局已经设置为边框布局。

Stack trace : 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
 at org.jfree.chart.renderer.category.GradientBarPainter.splitHorizontalBar(GradientBarPainter.java:321)
 at org.jfree.chart.renderer.category.GradientBarPainter.paintBar(GradientBarPainter.java:154)
 at org.jfree.chart.renderer.category.GanttRenderer.drawTask(GanttRenderer.java:574)
 at org.jfree.chart.renderer.category.GanttRenderer.drawTasks(GanttRenderer.java:321)
 at org.jfree.chart.renderer.category.GanttRenderer.drawItem(GanttRenderer.java:281)
 at org.jfree.chart.plot.CategoryPlot.render(CategoryPlot.java:3885)
 at org.jfree.chart.plot.CategoryPlot.draw(CategoryPlot.java:3653)
 at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1241)
 at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1650)
 at javax.swing.JComponent.paint(JComponent.java:1029)
 at javax.swing.JComponent.paintChildren(JComponent.java:862)
 at javax.swing.JComponent.paint(JComponent.java:1038)
 at javax.swing.JComponent.paintChildren(JComponent.java:862)
 at javax.swing.JComponent.paint(JComponent.java:1038)
 at javax.swing.JViewport.paint(JViewport.java:747)
 at javax.swing.JComponent.paintChildren(JComponent.java:862)
 at javax.swing.JComponent.paint(JComponent.java:1038)
 at javax.swing.JComponent.paintChildren(JComponent.java:862)
 at javax.swing.JSplitPane.paintChildren(JSplitPane.java:1030)
 at javax.swing.JComponent.paint(JComponent.java:1038)
 at javax.swing.JComponent.paintChildren(JComponent.java:862)
 at javax.swing.JComponent.paint(JComponent.java:1038)
 at javax.swing.JComponent.paintChildren(JComponent.java:862)
 at javax.swing.JComponent.paint(JComponent.java:1038)
 at javax.swing.JComponent.paintChildren(JComponent.java:862)
 at javax.swing.JComponent.paint(JComponent.java:1038)
 at javax.swing.JComponent.paintChildren(JComponent.java:862)
 at javax.swing.JComponent.paint(JComponent.java:1038)
 at javax.swing.JComponent.paintChildren(JComponent.java:862)
 at javax.swing.JComponent.paint(JComponent.java:1038)
 at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
 at javax.swing.JComponent.paintChildren(JComponent.java:862)
 at javax.swing.JComponent.paint(JComponent.java:1038)
 at javax.swing.JComponent.paintToOffscreen(JComponent.java:5124)
 at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1491)
 at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1422)
 at javax.swing.RepaintManager.paint(RepaintManager.java:1225)
 at javax.swing.JComponent._paintImmediately(JComponent.java:5072)
 at javax.swing.JComponent.paintImmediately(JComponent.java:4882)
 at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:786)
 at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
 at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:694)
 at javax.swing.RepaintManager.access$700(RepaintManager.java:41)
 at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1636)
 at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
 at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:646)
 at java.awt.EventQueue.access$000(EventQueue.java:84)
 at java.awt.EventQueue$1.run(EventQueue.java:607)
 at java.awt.EventQueue$1.run(EventQueue.java:605)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
 at java.awt.EventQueue.dispatchEvent(EventQueue.java:616)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
 at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
 at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
4

1 回答 1

0

我认为这与您提供的数据有关。在没有开始/结束的情况下运行您的示例,如下所示:

    double[][] starts = new double[0][]; 
    double[][] ends = new double[0][]; 
    createChart(new DefaultIntervalCategoryDataset(starts, ends));

不会导致任何错误。所以先试试这个,如果它没有失败,然后找到你添加到导致空指针问题的数据集的内容。

此外,可能不相关,我注意到setItemLabelGenerator您在渲染器对象上使用的已弃用。改为使用setSeriesItemLabelGenerator并指定您希望它申请的系列。

于 2013-11-06T16:10:05.510 回答