2014-09-02 159 views
0

问题: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) 
+0

您是否使用IDE进行开发? – reporter 2014-09-02 09:30:34

+0

你有异常堆栈跟踪吗? – 2014-09-02 09:33:11

+0

当你从'chart.draw((Graphics2D)emffile.create(),new Rectangle(1500,600))'中移除'.create()'时会发生什么? – 2014-09-02 09:35:12

回答

1

我在JFreeChart的论坛贴出this answer

这看起来像我在EMFGraphics2D类中的错误。空 指针异常是发生在行679:

678 private void writePen(BasicStroke stroke, Color color) throws IOException { 
679  if (color.equals(penColor) && stroke.equals(getStroke())) 

我没有运行代码,但据推测是“颜色”或“抚摸”是 空。由于代码行575到达这里,似乎 “色”是空项(因为“抚摸”不为空,因为它通过了测试的 实例):在所有

573 public void writeStroke(Stroke stroke) throws IOException { 
574  if (stroke instanceof BasicStroke) { 
575   writePen((BasicStroke) stroke, getColor());[/code] 

现在我见过的Graphics2D实现,getColor() 从来没有返回null(我已经写了测试用例来探讨这种行为)。但检查EMFGraphics2D代码,看起来好像 这是默认的任何调用setPaint()与非颜色 参数将重置颜色为空。我认为这是错误的。

+0

感谢您花时间分析问题。有没有解决问题的方法? – ming 2014-09-02 12:01:50

+1

我没有研究过所有的EMFGraphics2D代码,但是我会将该类更改为默认颜色(通过观察,Color.BLACK看起来是Graphics2D实例的通常默认值),然后确保无法设置颜色为空。 get/setColor()方法仅用于向后兼容AWT Graphics类,Graphics2D使用get/setPaint()方法。当设置不是Color的颜色(例如,GradientPaint)时,我发现'color'属性保持原样(而EMFGraphics2D看起来像将其设置为null)。 – 2014-09-02 16:01:40

+0

嗨大卫,我第一次使用了最新的jar文件。然后我切换到使用早期版本,它解决了这个问题。谢谢您的帮助。 – ming 2014-09-04 13:35:01

相关问题