problem rendering chart for saving

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jahjeremy
Posts: 31
Joined: Tue Sep 25, 2012 2:49 am
antibot: No, of course not.

problem rendering chart for saving

Post by jahjeremy » Fri Mar 27, 2015 9:58 pm

Hi,

I'm having an issue with painting a ChartPanel. This is using the JFreeChart 1.0.19 distribution.

Basically, there is a null pointer exception here....

Code: Select all

GraphicsConfiguration gc = g2.getDeviceConfiguration();
this.chartBuffer = gc.createCompatibleImage(this.chartBufferWidth, this.chartBufferHeight,
                        Transparency.TRANSLUCENT);
which is line 1594 of ChartPanel in the paintComponent method.

The problem seems to be that the Graphics2D object is returning a null device configuration, so the paint operation fails.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1601)
at javax.swing.JComponent.printComponent(JComponent.java:1218)
....

Can anyone discuss under what circumstances this would happen and how I might go about fixing it? Is there some method I can call to make sure this will work? Is it perhaps actually related to the state of the Graphics object that is being passed into this method rather than any bug or issue with ChartPanel?

If more details are needed about how this is being called then let me know. There is a separate external library I am using for actually saving the chart graphics, which I think is trying to paint the panel into its own graphics object and then persist it. I'm sure this has something to do with the problem I'm seeing. Perhaps there is a suggestion about how to go about doing this without causing these kinds of bugs, e.g. successfully exporting the ChartPanel graphics via this paint operation. (For technical reasons, it is difficult for me to instead use the built-in JFreeChart save command but I could also take a look at how that works too.)

By the way, rendering the chart graphics into my Swing application's components is working fine without any major issues.

Thanks.

--Jeremy

jahjeremy
Posts: 31
Joined: Tue Sep 25, 2012 2:49 am
antibot: No, of course not.

Re: problem rendering chart for saving

Post by jahjeremy » Fri Mar 27, 2015 10:43 pm

Okay, so this custom Graphics2D implementation that is being passed to the ChartPanel does not implement getDeviceConfiguration and hence the null pointer exception. Mystery solved, but I'm still wondering if there's a way to draw the chart onto an arbitrary Graphics2D without using this. Maybe I should turn off buffering...?

Locked