Rendering a Chart in a Servlet

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
TheHaas
Posts: 21
Joined: Sat Feb 07, 2004 9:14 pm
Contact:

Rendering a Chart in a Servlet

Post by TheHaas » Mon Feb 23, 2004 10:28 pm

I'm not a J2EE expert, but I'm having problems getting a chart from a servlet. I'm trying to follow the sample war but I keep getting a NullPointerException when it's trying to write the image file.

From my JSP page:

Code: Select all

	com.workpoint.client.ClientContext context = com.workpoint.client.ClientContext.createContext();
	
	context.open(sDSN,sResourceID,sPassword);
	
	String filename = ChartServlet.createJobStatusChart(context,"20040101","20040223",
						session, new PrintWriter(out));
My servlet:

Code: Select all

public class ChartServlet extends HttpServlet {
	
	public static String createJobStatusChart( ClientContext context, 
									String startDate, 
									String endDate, 
									HttpSession session,
									PrintWriter pw) {
										
		JFreeChart chart = JobStatusReport.getChart(context,startDate,endDate);
		return createChartImage(session,pw,chart);
										
	}
	public static String createChartImage(HttpSession session,
										 PrintWriter pw,
										 JFreeChart chart){
		String filename = "";
		
		
		ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); 
		
		try {
		
			filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
		// Write the image map to the PrintWriter 
			ChartUtilities.writeImageMap( pw, filename, info);
		} catch (IOException e1) {
			
			e1.printStackTrace();
		} 
		pw.flush();
		 
		return filename;
		
	}
}
The exception:

Code: Select all

java.lang.NullPointerException
	at org.jfree.chart.ChartUtilities.writeChartAsPNG(Unknown Source)
	at org.jfree.chart.ChartUtilities.saveChartAsPNG(Unknown Source)
	at org.jfree.chart.ChartUtilities.saveChartAsPNG(Unknown Source)
	at org.jfree.chart.servlet.ServletUtilities.saveChartAsPNG(Unknown Source)
	at com.workpoint.reports.charts.ChartServlet.createChartImage(ChartServlet.java:62)
	at com.workpoint.reports.charts.ChartServlet.createJobStatusChart(ChartServlet.java:49)
I can't see any difference between what I'm doing (which doesn't work) an d the example WAR (which does work).

Anyone have any ideas??

Locked