Getting an exception when handling multiple datasets

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
evan
Posts: 14
Joined: Thu Feb 08, 2007 6:26 pm

Getting an exception when handling multiple datasets

Post by evan » Thu Feb 08, 2007 6:35 pm

Hi,

I have, say 3 datasets in the plot, and from time to time, I need to add/remove them from the plot. It works fine with adding/removing the other two datasets, but whenever I try to remove the primary dataset, I get an NullPointerException as following:

Code: Select all

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at org.jfree.chart.plot.XYPlot.drawRangeGridlines(XYPlot.java:2791)
	at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:2202)
	at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1039)
	at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1269)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
	at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
	at javax.swing.JComponent._paintImmediately(Unknown Source)
	at javax.swing.JComponent.paintImmediately(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
The code I use to remove a set from the plot is as following.

Code: Select all

plot.setDataset(i, null);
plot.setRenderer(i, null);
Is there anything special I need to take care of for the primary dataset? The API says null is permitted tho. Could someone please help me with this problem? Many thanks.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Sat Feb 10, 2007 7:17 am

I fixed a similar bug in the 1.0.4 release which was posted yesterday. Can you test with that, and let me know if it works. If the bug is still there on 1.0.4, I'll take a closer look.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

evan
Posts: 14
Joined: Thu Feb 08, 2007 6:26 pm

Post by evan » Sun Feb 11, 2007 4:21 pm

david.gilbert wrote:I fixed a similar bug in the 1.0.4 release which was posted yesterday. Can you test with that, and let me know if it works. If the bug is still there on 1.0.4, I'll take a closer look.
Thanks david.

I've tried it again with the latest 1.0.4, but unfortunately, the problem still persists. Again, it only happens with the primary dataset (datasets[0]), but not the other ones. The following is the exception returned by 1.0.4.

Code: Select all

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at org.jfree.chart.plot.XYPlot.drawRangeGridlines(XYPlot.java:3013)
	at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:2420)
	at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1041)
	at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1229)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
	at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
	at javax.swing.JComponent._paintImmediately(Unknown Source)
	at javax.swing.JComponent.paintImmediately(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Can you please take a look and let me know if you find anything.

evan
Posts: 14
Joined: Thu Feb 08, 2007 6:26 pm

Post by evan » Fri Feb 23, 2007 10:19 pm

Hi dave,

Have you taken any time to look into this potential bug? Please let me know if you find anything. Many thanks.

evan
Posts: 14
Joined: Thu Feb 08, 2007 6:26 pm

Post by evan » Mon Mar 05, 2007 12:08 am

Has anyone else experienced the similar problem or is it just me who's got this wrong? Thanks.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Mar 05, 2007 11:48 am

evan wrote:Have you taken any time to look into this potential bug? Please let me know if you find anything. Many thanks.
Thanks for the reminder. I've just committed this change to CVS, for inclusion in the 1.0.5 release:

Code: Select all

Index: source/org/jfree/chart/plot/XYPlot.java
===================================================================
RCS file: /cvsroot/jfreechart/jfreechart/source/org/jfree/chart/plot/XYPlot.java,v
retrieving revision 1.44.2.22
diff -u -r1.44.2.22 XYPlot.java
--- source/org/jfree/chart/plot/XYPlot.java	2 Mar 2007 11:26:56 -0000	1.44.2.22
+++ source/org/jfree/chart/plot/XYPlot.java	5 Mar 2007 10:42:18 -0000
@@ -3077,6 +3077,11 @@
     protected void drawRangeGridlines(Graphics2D g2, Rectangle2D area,
                                       List ticks) {
 
+        // no renderer, no gridlines...
+        if (getRenderer() == null) {
+            return;
+        }
+
         // draw the range grid lines, if any...
         if (isRangeGridlinesVisible()) {
             Stroke gridStroke = getRangeGridlineStroke();
Index: tests/org/jfree/chart/plot/junit/XYPlotTests.java
===================================================================
RCS file: /cvsroot/jfreechart/jfreechart/tests/org/jfree/chart/plot/junit/Attic/XYPlotTests.java,v
retrieving revision 1.1.2.5
diff -u -r1.1.2.5 XYPlotTests.java
--- tests/org/jfree/chart/plot/junit/XYPlotTests.java	7 Feb 2007 16:31:50 -0000	1.1.2.5
+++ tests/org/jfree/chart/plot/junit/XYPlotTests.java	5 Mar 2007 10:42:19 -0000
@@ -538,9 +538,8 @@
     public void testSetNullRenderer() {
         boolean failed = false;
         try {
-            XYPlot plot = new XYPlot(
-                null, new NumberAxis("X"), new NumberAxis("Y"), null
-            );
+            XYPlot plot = new XYPlot(null, new NumberAxis("X"), 
+                    new NumberAxis("Y"), null);
             plot.setRenderer(null);
         }
         catch (Exception e) {
@@ -956,5 +955,30 @@
         }
         assertTrue(success);
     }
-        
+
+    /**
+     * A test for drawing range grid lines when there is no primary renderer.
+     * In 1.0.4, this is throwing a NullPointerException.
+     */
+    public void testDrawRangeGridlines() {
+        DefaultXYDataset dataset = new DefaultXYDataset();
+        JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y",
+                dataset, PlotOrientation.VERTICAL, true, false, false);
+        XYPlot plot = (XYPlot) chart.getPlot();
+        plot.setRenderer(null);
+        boolean success = false;
+        try {
+            BufferedImage image = new BufferedImage(200 , 100, 
+                    BufferedImage.TYPE_INT_RGB);
+            Graphics2D g2 = image.createGraphics();
+            chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
+            g2.dispose();
+            success = true;
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+            success = false;
+        }
+        assertTrue(success);
+    }
 }
\ No newline at end of file
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

evan
Posts: 14
Joined: Thu Feb 08, 2007 6:26 pm

Post by evan » Tue Mar 06, 2007 1:45 am

Thanks dave!

I've retrieved the latest XYPlot from the CVS and tried it out. After removing the primary set, it doesn't throw NullPointerException anymore, however, the gridlines at the background are gone with primary set but as soon as I add it back, the gridlines re-appear.

I wonder if this is desired behaviour of this fix or the fix triggers some other bug? Thanks.
After looking at the code a bit closer, it turns out this behaviour is desired result in order to avoid throwing the NullPointerException. My apology for not reading the code in the first place. Thanks for the fix.

Locked