我尝试使用JasperReports通过我的应用程序中的serlvet生成pdf发票。我通过eclipse安装了JasperReports插件,并且一直在使用它运行,出于某种原因,虽然我的应用程序不会打印出我的pdf。我得到的空指针似乎无法解决。
这是我的代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//String type = request.getParameter("type");
//int id = Integer.parseInt(request.getParameter("id"));
String jrxmlFileName = "C:/res/jasper/OrderInvoice.jrxml";
String jasperFileName = "C:/res/jasper/OrderInvoice.jasper";
String pdfFileName = "C:/res/pdfs/invoice01.pdf";
try
{
JasperCompileManager.compileReportToFile(jrxmlFileName, jasperFileName);
// String dbUrl = props.getProperty("jdbc.url");
String dbUrl = "jdbc:mariadb://localhost:3306/*MY DATABASE*";
// String dbDriver = props.getProperty("jdbc.driver");
String dbDriver = "org.mariadb.jdbc.Driver";
// String dbUname = props.getProperty("db.username");
String dbUname = "*MY DATABASE USERNAME*";
// String dbPwd = props.getProperty("db.password");
String dbPwd = "*MY DATABASE PASSWORD*";
// Load the JDBC driver
Class.forName(dbDriver);
// Get the connection
Connection conn = DriverManager.getConnection(dbUrl, dbUname, dbPwd);
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("OrderId", 1);
JasperPrint jprint = JasperFillManager.fillReport(jasperFileName, hm, conn).setJasperReportsContext(jasperReportsContext);;
JasperExportManager.exportReportToPdfFile(jprint, pdfFileName);
conn.close();
} catch (JRException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
我生成的报告包含一个参数,即为其创建发票的特定订单的订单ID。这是HashMap中设置的内容。我在JasperReports页面上找到了这段代码。
这是我收到的空指针:
java.lang.NullPointerException
at com.jaspersoft.studio.debug.TraceGovernor.setup(TraceGovernor.java:111)
at com.jaspersoft.studio.debug.TraceGovernor.initMaps(TraceGovernor.java:118)
at com.jaspersoft.studio.debug.TraceGovernor.beforeReportInit(TraceGovernor.java:162)
at net.sf.jasperreports.engine.fill.JRFillDatasetScriptlet.beforeReportInit(JRFillDatasetScriptlet.java:74)
at net.sf.jasperreports.engine.JRAbstractScriptlet.callBeforeReportInit(JRAbstractScriptlet.java:176)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:236)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:115)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:582)
at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:396)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:90)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:286)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:759)
at exchange.InvoiceExchange.doGet(InvoiceExchange.java:68)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1681)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:466)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:169)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:526)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)
我找到了这个页面How can I fix java.lang.NullPointerException caused by TraceGovernor in Jaspersoft studio plugin? 但我无法完成他找到的解决方案。
我还在线尝试了一些不同的示例代码,但都没有。