Struts Web 应用程序中的 JasperReport 错误

发布于 2024-10-30 21:06:42 字数 8293 浏览 3 评论 0原文

我在基于 Struts 的 Web 应用程序中使用 JasperReport 以及 NetBeans 6.8 和 IReport 插件 3.7.4。我在操作类中收到以下错误:

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(ObjectInputStream.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)

我在操作类中使用了以下代码:

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;
    }

请建议我哪里出错了。我是第一次使用 JasperReport,束手无策:(

更新

我认为问题出在报告本身。即使是简单的代码也会出现错误:

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;
}

这次出现不同的错误消息:

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)

但是当我在 IReport 设计器中查看报表时,预览会正确显示,请帮助启动报表。

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(ObjectInputStream.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森末i 2024-11-06 21:06:42

警告:我不是 struts 开发人员(更多的是 JSF 1.2/2.0):

JasperReports 相当挑剔,而且错误信息非常冗长,除非您专门将其配置为静默运行。空指针异常通常来自您不期望的地方(图像/简单文本等...),不幸的是有时您会在“loadObject”或“fillReport”调用中收到很少或没有信息的错误。

尝试将其分解为一系列步骤,并在调试器中查看它以准确了解失败的位置。一旦你把它拆开,这可能是理所当然的。删除所有图像等...并运行空白报告(字面上是一个 hello world),以确保 NPE 不在报告内并且您的框架正常工作。一旦你把它分成几个部分,调试起来就会很容易。

通常你可以检查每个步骤:

//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); 

我真的不建议在生产环境中编译(使用打印是最好的方法),但我遇到了一系列类加载问题(JBoss相关),由图像和其他细节引起的NPE以及报告中各种不良脚本引起的错误。

在框架内进行测试,您很快就会发现问题。 Jasper 可以说是最好的 Java 报告软件(非常详细),但调试起来却相当令人恼火。

通过将其分解为步骤,可以更轻松地调试并查看 NPE 实际存在的位置。您使用的代码似乎没问题:我会将钱放在空流或报告中未找到的资源(可能是图像)上。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文