JasperReport error in Struts web Application
I am using JasperReport in a Struts-based web application with NetBeans 6.8 and IReport plug-in 3.7.4. I am getting the following error in the action class:
java.lang.NullPointerException
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2266)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2279)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
at java.io.ObjectInputStream.readStreamHeader(ObjectIn开发者_StackOverflowputStream.java:780)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:188)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:381)
at net.sf.jasperreports.engine.JasperRunManager.runReportToPdfStream(JasperRunManager.java:186)
at com.bspl.psys.ilp.action.ReportAppLinkAction.execute(ReportAppLinkAction.java:65)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:619)
I have used the following code in action class:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
ServletOutputStream servletOutputStream = response.getOutputStream();
InputStream reportStream = getServlet().getServletConfig().getServletContext().getResourceAsStream("SampleReport.jasper");
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/SampleDB?user=common&password=common");
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream,new HashMap());
connection.close();
response.setContentType("application/pdf");
response.reset();
reportStream.close();
servletOutputStream.flush();
servletOutputStream.close();
} catch (Exception ex) {
ex.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
return null;
}
Please suggest me as to where I am going wrong. I am using JasperReport for the first time and at my wits end :(
Update
I think the problem is in the report itself. Even simple code as this is giving error:
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/SampleDB?user=common&password=common);
JasperFillManager.fillReportToFile("D:/dropbox/report/SampleReport.jasper",new HashMap(), connection);
System.out.println("Done!");
} catch (Exception ex) {
ex.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
return null;
}
This time a different error message comes up:
java.lang.NullPointerException
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:63)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:402)
at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:188)
at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:94)
at com.bspl.psys.ilp.action.ReportAppLinkAction.execute(ReportAppLinkAction.java:84)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:619)
But when I view the report in IReport designer the preview shows up correctly. Please help. It is taking way too long just to kick-off reporting.
Caveat: I'm not a struts developer (more JSF 1.2/2.0):
JasperReports is pretty finicky and very verbose on errors unless you specifically configure it to behave silently. The null pointer exceptions often come from places you're not expecting (images/simple text etc...) and unfortunately sometimes you get the error in the "loadObject" or "fillReport" calls with little or no information.
Try breaking it apart into a series of steps and looking at it in the debugger to see exactly where it fails. This is likely a no-brainer once you get it broken apart. Remove all images etc... and run a blank report (literally a hello world) to make sure the NPE isn't inside the report and that your framework works. It is going to be pretty easy to debug once you have it in parts.
Typically you can check each step with:
//load == classloader (I'm inside JBoss so I have to explicitly define it)
//This is pseudo code pulled from my test environment
JasperReport report = JasperCompileManager.compileReport(load.getResourceAsStream("yourpackage/report.jrxml"));
JasperPrint print = JasperFillManager.fillReport(report, new Map<String, Object>(), java.sql.Connection);
JasperExportManager.exportReportToPdfStream(print, outStream);
I really don't recommend compiling in a production environment (using the print is the best way), but I have had a series of classloading problems (JBoss related), NPE's caused by Images and other minutia and errors caused by a variety of bad scripts inside the report.
Get a test inside the framework and you'll find the problem very quickly. Jasper is arguably the best reporting software for Java out there (it is immensely detailed) but it is pretty infuriating to debug.
By breaking it into steps it makes it easier to debug and see where the NPE actually exists. The code you use appears to be fine: I'd place my money on a null stream or a resource in the report that isn't located (likely an image).
精彩评论