問題:chart.draw((Graphics2D) emffile.create(), new Rectangle(1500, 600))
拋出Nullpointerexception。Jfreechart.draw()拋出Nullpointerexception
可以生成條形圖。請幫忙看看這個問題。
這裏我的代碼:
CategoryDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
emffile = new EMFGraphics2D(
new File("C:\\Workspace\\eclipse\\MSReoprt\\chart.emf"),
new Dimension(1500, 600)
);
emffile.setDeviceIndependent(true);
emffile.setRenderingHint(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY
);
emffile.setRenderingHint(
RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_NORMALIZE
);
emffile.startExport();
chart.draw((Graphics2D) emffile.create(), new Rectangle(1500, 600));
emffile.endExport();
emffile.closeStream();
類別和jfreechart的方法:
private CategoryDataset createDataset()
{
String series = "Availability";
String category1 = "Portal";
String category2 = "DB";
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, series, category1);
dataset.addValue(90, series, category2);
return dataset;
}
public JFreeChart createChart(CategoryDataset dataset)
{
FreeChart chart = ChartFactory.createBarChart
(
"Bar Chart Demo", //chart title
"Category", //domain axis label
"", //range axis label
dataset, //data
PlotOrientation.VERTICAL, //orientation
true, //include legend
true, //tooltips?
false //URLs?
);
//set the background color for the chart...
chart.setBackgroundPaint(Color.white);
//get a reference to the plot for further customisation...
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.gray);
//set the range axis to display integers only...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//disable bar outlines...
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
renderer.setMaximumBarWidth(0.5);
renderer.setItemMargin(4);
//set up gradient paints for series...
GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, Color.MAGENTA
);
//GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green,
//0.0f, 0.0f, Color.lightGray);
//GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red,
//0.0f, 0.0f, Color.lightGray);
renderer.setSeriesPaint(0, gp0);
//renderer.setSeriesPaint(1, gp1);
//renderer.setSeriesPaint(2, gp2);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI/6.0)
);
return chart;
}
添加堆棧跟蹤:
Exception in thread "main" java.lang.NullPointerException
at org.freehep.graphicsio.emf.EMFGraphics2D.writePen(EMFGraphics2D.java:679)
at org.freehep.graphicsio.emf.EMFGraphics2D.writeStroke(EMFGraphics2D.java:575)
at org.freehep.graphicsio.AbstractVectorGraphicsIO.setStroke(AbstractVectorGraphicsIO.java:981)
at org.jfree.chart.plot.Plot.drawOutline(Plot.java:1125)
at org.jfree.chart.renderer.category.AbstractCategoryItemRenderer.drawOutline(AbstractCategoryItemRenderer.java:717)
at org.jfree.chart.plot.CategoryPlot.draw(CategoryPlot.java:3684)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1229)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1112)
at chart.BarChartDemo.export(BarChartDemo.java:61)
at chart.BarChartDemo.test(BarChartDemo.java:39)
at chart.BarChartDemo.main(BarChartDemo.java:137)
您是否使用IDE進行開發? – reporter 2014-09-02 09:30:34
你有異常堆棧跟蹤嗎? – 2014-09-02 09:33:11
當你從'chart.draw((Graphics2D)emffile.create(),new Rectangle(1500,600))'中移除'.create()'時會發生什麼? – 2014-09-02 09:35:12