当前位置:编程学习 > JAVA >>

Java调用jasper抛空指针异常

jasper是用IReport3.7生成的,生成的时候没报错
jasper文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Test2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="62" y="31" width="100" height="20"/>
<textElement/>
<text><![CDATA[Static text]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch"/>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>

Jsp代码如下:
<%
      //报表编译之后生成的.jasper 文件的存放位置
      File reportFile = new File(application.getRealPath("ireport/Test2.jasper"));
      //这个是用来联接我的mysql 的JDBC URL
      String url="jdbc:oracle:thin:@localhost:1521:orcl";
      Class.forName("oracle.jdbc.driver.OracleDriver");
      //传递报表中用到的参数值
      Map<String,Object> parameters = new HashMap<String,Object>();
      parameters.put("NAME","aa");
     //"Name"是报表中定义过的一个参数名称,其类型为String 型
     System.out.println("---------conn-------------");
     //连接到数据库
     Connection conn = DriverManager.getConnection(url,"majian","majian");
     System.out.println("---------Jasper begin-------------");
     //在控制台显示一下报表文件的物理路径
     System.out.println(reportFile.getPath());
     byte[] bytes=JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,conn);
 System.out.println("run");
     System.out.println("---------Jasper end-------------");
     response.setContentType("application/pdf");
     response.setContentLength(bytes.length);
     ServletOutputStream ouputStream = response.getOutputStream();
     ouputStream.write(bytes, 0, bytes.length);
     ouputStream.flush();
     ouputStream.close();
     out.clear();
     out=pageContext.pushBody();
   %>
输出如下:
---------conn-------------
---------Jasper begin-------------
E:\TomCat\Tomcat6.0.18\webapps\IReport\ireport\Test2.jasper
2012-6-7 23:06:08 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
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.fillReport(JasperFillManager.java:234)
at net.sf.jasperreports.engine.JasperRunManager.runReportToPdf(JasperRunManager.java:203)
at org.apache.jsp.ireport2_jsp._jspService(ireport2_jsp.java:107)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
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.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:128)
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:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619) --------------------编程问答-------------------- 你用IReport画图的时候用的Connection跟程序部署之后的数据库是同一个库么。 --------------------编程问答-------------------- tomcat work目录下ireport2_jsp.java:107 行,查查 --------------------编程问答-------------------- 是同一个数据库啊 --------------------编程问答-------------------- 我在IReport中根本就没用数据库,用的是空数据源,如果我在Java代码中不传递这个Connection对象,也是抛空指针异常 --------------------编程问答-------------------- Java代码如下:
<%
      //报表编译之后生成的.jasper 文件的存放位置
      File reportFile = new File(application.getRealPath("ireport/Test2.jasper"));
      //这个是用来联接我的mysql 的JDBC URL
     // String url="jdbc:oracle:thin:@localhost:1521:orcl";
      //Class.forName("oracle.jdbc.driver.OracleDriver");
      //传递报表中用到的参数值
      Map<String,Object> parameters = new HashMap<String,Object>();
      parameters.put("NAME","aa");
     //"Name"是报表中定义过的一个参数名称,其类型为String 型
     System.out.println("---------conn-------------");
     //连接到数据库
    // Connection conn = DriverManager.getConnection(url,"majian","majian");
     System.out.println("---------Jasper begin-------------");
     //在控制台显示一下报表文件的物理路径
     System.out.println(reportFile.getPath());
     byte[] bytes=JasperRunManager.runReportToPdf(reportFile.getPath(),parameters);
 System.out.println("run");
     System.out.println("---------Jasper end-------------");
     response.setContentType("application/pdf");
     response.setContentLength(bytes.length);
     ServletOutputStream ouputStream = response.getOutputStream();
     ouputStream.write(bytes, 0, bytes.length);
     ouputStream.flush();
     ouputStream.close();
     out.clear();
     out=pageContext.pushBody();
   %>

异常如下:
java.lang.NullPointerException
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:123)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:420)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:256)
at net.sf.jasperreports.engine.JasperRunManager.runReportToPdf(JasperRunManager.java:225)
at org.apache.jsp.ireport2_jsp._jspService(ireport2_jsp.java:107)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
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.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:128)
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:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619) --------------------编程问答-------------------- LZ 发的是jsp 文件代码,应该发将该jsp编译为对应java文件,代码中定位具体的错误的行号?

参照
http://zhidao.baidu.com/question/153733298.html

--------------------编程问答-------------------- 抛出空指针的代码是:
byte[] bytes=JasperRunManager.runReportToPdf(reportFile.getPath(),parameters);
问题是reportFile.getPath()和parameters都不为空 --------------------编程问答-------------------- 将
File reportFile = new File(application.getRealPath("ireport/Test2.jasper"));
替换成

File reportFile = new File(application.getRealPath("ireport/Test2.jrxml"));

问题解决
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,