0
votes

I'm coding a Gantt chart using JFreechart - The chartPanel was created and added to an existing JPanel. However, after the JPanel validate() method, am seeing the NullPointerException in GradientBarPainter.splitHorizontalBar()

The exception is at the end of the post. Any idea of how to solve this error?

--------------------------
 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();


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

The JPanel "rightPanel" was added using Netbeans, and the Layout was set as borderlayout already.

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)
1
It's hard at least for me to guess what might be wrong. I'll take a wild stab. Are you setting the look and feel for the GUI? Are you taking care to start the GUI on the Swing event thread? - Hovercraft Full Of Eels
I'd suggest that best would be for you to create and post an sscce, but that will be hard to do when using a 3rd party library. Keep whittling down your program in an effort to isolate the error. - Hovercraft Full Of Eels
What version of JFreeChart are you using? - Nathaniel Jones
You seem to be using an old version, because the latest GanttRenderer has no line 574. - Russell Zahniser

1 Answers

0
votes

I think it has something todo with the data you provided. Running your example with no starts/ends like so:

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

Does not result in any error. So try this out first and if it doesn't fail then find what you are adding to the dataset that is causing the null pointer issue.

Also, possibly unrelated, I noticed the setItemLabelGenerator you are using on your renderer object is deprecated. Use setSeriesItemLabelGenerator instead and specify the series you want it to apply for.