2013-09-26 178 views
0

report.java什麼和得到錯誤空指針錯誤碧玉報告顯示的NetBeans

 byte[] pdfbyte = null; 
     try { 
      dbcon connect = new dbcon(); 
      ServletContext context = this.getServletConfig().getServletContext(); 
      String compiledFileName = context.getRealPath("/jasper/report3.jasper"); 
      System.out.print(compiledFileName); 
      // String var = request.getParameter("var").toString(); 
      //String res = JOptionPane.showInputDialog(null, "What is your name?", "Enter your name",JOptionPane.QUESTION_MESSAGE); 

      File reportFile = new File(compiledFileName); 
      HashMap parameters = new HashMap(); 
//   // parameters.put("itemName", var); 
      parameters.put("ReportTitle", "MyReport"); 
//   parameters.put("BaseDir", reportFile.getParentFile()); 
//   parameters.put("EmpNo", 32504); 
      JasperPrint jasperPrint = null; 
      Connection conn = connect.dbstate().getConnection(); 
      try { 

       try { 
        jasperPrint = JasperFillManager.fillReport(compiledFileName, parameters, conn); 
        pdfbyte = JasperExportManager.exportReportToPdf(jasperPrint); 
       } catch (Exception e) { 
        System.out.print("E" + e); 
       } 
      } catch (Exception ex) { 
       System.out.println("*** Could not create compiled jasper file: " + ex.getMessage()); 
      } finally { 
       try { 
        connect.dbstate().close(); 
       } catch (SQLException ex) { 
        System.out.println("could not close databse after filling the report "); 
       } 
      } 
      ServletOutputStream outstream = response.getOutputStream(); 

      response.setContentType("application/pdf"); 
      response.setContentLength(pdfbyte.length); 
      response.setHeader("Content-disposition", "inline; filename=\"Report.pdf\""); 
      outstream.write(pdfbyte); 
     } catch (Exception e) { 
      System.out.print(e); 
      e.printStackTrace(); 
     } 

圖書館

enter image description here

我得到的NetBeans

SEVERE: A web application registered the JBDC driver [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. 
Ejava.lang.NullPointerExceptionjava.lang.NullPointerExceptionjava.lang.NullPointerException 
     at jasper.Report.processRequest(Report.java:63) 
     at jasper.Report.doGet(Report.java:75) 
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
     at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) 
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) 
     at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859) 
     at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579) 
     at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555) 
     at java.lang.Thread.run(Thread.java:619) 
以下錯誤
+0

該報告是在'pdfFilePath'生成的嗎? - 另外,個人而言,我會避免在運行時編譯報告,因爲這是一個相當緩慢的過程,您應該能夠預編譯,然後只需使用輸出來填充報告... – MadProgrammer

+0

是的..我想要下載/生成報告到服務器文件夾'jasperReport'並且瀏覽器上應該顯示相同的時間報告。 – Ravi

+0

您可以將報告輸出到HTML並將結果輸出到服務器。否則,你應該嘗試提供鏈接...我想,自從我嘗試過這樣的事情之後,我有一段時間了) – MadProgrammer

回答

0

有沒有與任何代碼的問題,我改變所有的庫,最新更新的一個像jasperreport-4.5.1.jarjasperreport-5.1.0.jar和它的工作!

1

我建議你預編譯你的報告,就像@MadProgrammer告訴的那樣。要發送servlet的一份報告,在客戶端(previwe在網頁瀏覽器或直接下載),我用這個:

... 
// trying get the report 
     try { 

     // getting de real path 
     String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath(reportFileName); 

     // 
     HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); 

     // here you can specify the condition to print, if conditionVoolean is true it will do a direct download 
     if (conditionBoolean) { 
      fileName = "your file name to direct download"; 
      httpServletResponse.addHeader("Content-disposition", "attachment; filename=" + fileName +".pdf"); 
     } 

     // 
     byte[] bytes; 

     // get the filled report in bytes 
     bytes = JasperRunManager.runReportToPdf(jasperReport, parameter, conn); 

     if (bytes != null && bytes.length > 0) { 

      // specifying the type of file/content to browser 
      httpServletResponse.setContentType("application/pdf"); 

      httpServletResponse.setContentLength(bytes.length); 

      ServletOutputStream ouputStream = httpServletResponse.getOutputStream(); 

      ouputStream.write(bytes, 0, bytes.length); 

      ouputStream.flush(); 

      // closing the stream line 
      ouputStream.close(); 
     } 
     FacesContext.getCurrentInstance().responseComplete(); 
    } 
    catch (JRException ex) { 
     ex.printStackTrace(); 
    } 
    catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
... 

ATT:對不起,葡萄牙語意見。它是我的母語。

希望它可以幫助^^

+0

您的解決方案沒有幫助。我仍然變得空白。沒有生成pdf。 – Ravi

+0

好吧,讓我們嘗試解決這個問題。例外在那裏繼續?如果你的結果集是空的,它將顯示一個空白報告。確認你的查詢兄弟? – Cold

+0

感謝兄弟,我檢查了所有和昨天谷歌搜索,發現實際問題。這是因爲我正在使用一些舊版本庫。我下載了最新的一個它工作了!所以,我猜我的代碼是正確的。 – Ravi