2011-10-26 26 views
0

我正在使用iReport-4.1.1創建.jasper和.jrxml文件。我的文件在Webcontent/reports以XML作爲輸入的JasperReports --- Getiing Inputstream空指針異常

下面是我用來從jasper打開PDF的代碼。我將XML作爲輸入。

InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/reports/FUECReport.jasper"); 
JRXmlDataSource xmlDataSource = new JRXmlDataSource(stringToDom(xmlResult)); 

HashMap parameterMap = new HashMap(); 
parameterMap.put("TITLE_MSG_PARAM", fUECRptMsg); 
parameterMap.put("SURVEY_YEAR_PARAM", surveyYear);    
parameterMap.put("STATE_CODE_PARAM", fipsStateCode); 

JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream, parameterMap,xmlDataSource); 
response.setContentType("application/pdf"); 
servletOutputStream.flush(); 
servletOutputStream.close(); 

//this is the function that is converting the xmlsource to Document 
public static Document stringToDom(String xmlSource) throws SAXException, ParserConfigurationException, IOException { 
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder builder = factory.newDocumentBuilder(); 
    return builder.parse(new InputSource(new StringReader(xmlSource))); 
} 

當我跑了,我在該行得到一個空指針異常:

JasperRunManager.runReportToPdfStream(*reportStream*,servletOutputStream, parameterMap,xmlDataSource); 

說reportStream是空

有人請讓我知道問題出在哪裏。

當我從ireport運行並編譯時,我能夠成功打開PDF。

感謝

GK

回答

1

首先,你應該檢查路徑一致的報告模板:

InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/reports/FUECReport.jasper"); 

可能是你應該改變路徑/WEB-INF/reports/FUECReport.jasper?您可以嘗試在java.io的幫助下檢查文件的可用性。

第二,變量reportStream必須包含compiled報告。

javadoc不說這個方法(你可以看到與another signature同樣的方法參數),但你可以查看該方法的源代碼:

runReportToPdfStream

public static void JasperRunManager.runReportToPdfStream(java.io.InputStream inputStream, 
            java.io.OutputStream outputStream, 
            java.util.Map<java.lang.String,java.lang.Object> parameters, 
            JRDataSource jrDataSource) 
          throws JRException 
          throws JRException 

- 填寫報告並將其直接發送到PDF格式的OutputStream。中間的JasperPrint對象不保存在磁盤上。

請檢查您的數據源 - 準備另一個數據源並將其傳遞給runReportToPdfStream方法。

您可以使用JRMapArrayDataSourceJRMapArrayDataSource樣品使用:

 Map<String, Object> params = new HashMap<String, Object>(); 
     params.put("Title", "Report title"); 

     Map<String, String> firstRow = new HashMap<String, String>(); 
     firstRow.put("title", "Greatest Hits"); 
     firstRow.put("artist", "Dolly Parton"); 
     firstRow.put("country", "USA"); 
     firstRow.put("company", "RCA"); 
     firstRow.put("price", "9.90"); 
     firstRow.put("year", "1982"); 

     Map<String, String> secondRow = new HashMap<String, String>(); 
     secondRow.put("title", "Still got the blues"); 
     secondRow.put("artist", "Gary Moore"); 
     secondRow.put("country", "UK"); 
     secondRow.put("company", "Virgin records"); 
     secondRow.put("price", "10.20"); 
     secondRow.put("year", "1990"); 

     Object[] data = {firstRow, secondRow}; 

     JRMapArrayDataSource dataSource = new JRMapArrayDataSource(data); 

     JasperReport jasperReport = JasperCompileManager.compileReport(reportSource); 
     JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource); 

     JasperExportManager.exportReportToPdfFile(jasperPrint, targetFileName); 

如果一切都會好的,在方法的問題,你準備的數據源。

+0

FUECReport.jasper文件位於正確的位置。我將InputStream對象從reportStream更改爲reportStream1。我還使用InputStreamReader來檢查輸入流是否正確。這是正確的,我檢查。現在我得到一個異常,net.sf.jasperreports.engine.JRException:從InputStream加載對象時出錯........引起:java.io.EOFException – user1015388

+0

我能夠克服這個問題,但現在PDF打開(從runReportToPdfStream)所有的值爲NULL。我在PDF中看不到任何數據。該列的所有值均爲NULL。 – user1015388

+0

你檢查參數圖嗎?你看到靜態文本了嗎? –