Jfreechart.draw()抛出一个Nullpointerexception

时间:2014-09-02 09:18:29

标签: java jfreechart

问题:chart.draw((Graphics2D) emffile.create(), new Rectangle(1500, 600))抛出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)

1 个答案:

答案 0 :(得分:1)

我在JFreeChart论坛上发布了this answer

  

这对我来说就像是EMFGraphics2D类中的一个错误。 null   指针异常发生在第679行:

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

我没有运行代码,但可能是“颜色”或“笔画”   空值。由于代码从第575行到达这里,似乎就是这样   'color'是空项(因为'stroke'不是null,因为它传递了   测试的实例):

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

现在在我见过的所有Graphics2D实现中,getColor()   从不返回null(我编写测试用例来探索此行为)。但是检查EMFGraphics2D代码,似乎就好了   这是使用非Color调用setPaint()的默认 plus   参数将颜色重置为null。我认为这是错误的。