2014-09-02 156 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

相關問題