我正在使用动态报告,而动态报告又使用 Jasper Reports 生成 pdf。我的代码中出现 ClassCastException,因为我无法理解其中的任何内容。如果有人知道错误可能出在哪里,请帮助我。很抱歉,如果我错过任何细节,因为我赶时间。随时询问任何需要的额外细节。
这是发生错误的代码
public void createPdf(OutputStream out) {
SubreportBuilder subreport = cmp.subreport(new SubreportExpression())
.setDataSource(new SubreportDataSourceExpression());
JasperPdfExporterBuilder pdfExporter = Exporters.pdfExporter("D:\\PDF\\report.pdf");
try {
report()
.detail(
subreport,
cmp.verticalGap(20))
.pageHeader(PdfTemplate.headerPortraitComponent)
.pageFooter(PdfTemplate.footerPortraitComponent)
.setDataSource(createDataSource())
.toPdf(out);
} catch (Exception e) {
e.printStackTrace();
}
}
这是错误输出
Jun 28, 2013 5:09:32 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [CMCaseServlet] in context with path [] threw exception
net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :
Source text : $F{column1}
at net.sf.jasperreports.engine.fill.JRFillSubreport.prepare(JRFillSubreport.java:782)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:331)
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:384)
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:358)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2046)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:778)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportContent(JRVerticalFiller.java:308)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:155)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:841)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:88)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:653)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:969)
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperPrint(JasperReportBuilder.java:308)
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.show(JasperReportBuilder.java:327)
at cliff.reports.CMCaseReport.createPdf(CMCaseReport.java:58)
at cliff.servlet.CMCaseServlet.processRequest(CMCaseServlet.java:366)
at cliff.servlet.CMCaseServlet.doGet(CMCaseServlet.java:611)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at cliff.filter.LoginFilter.doFilter(LoginFilter.java:37)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1812)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :
Source text : $F{column1}
at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:203)
at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:591)
at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:559)
at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:966)
at net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java:456)
at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:440)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:259)
at net.sf.jasperreports.engine.fill.JRFillFrame.evaluate(JRFillFrame.java:149)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:259)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:455)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2044)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:778)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:288)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:151)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:841)
at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:655)
at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59)
at net.sf.jasperreports.engine.fill.AbstractThreadSubreportRunner.run(AbstractThreadSubreportRunner.java:203)
... 3 more
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at Report_1372410572739_904220.evaluate(Report_1372410572739_904220:202)
at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:190)
... 21 more
更新
其他方法的代码
private static JRDataSource createDataSource() {
return new JREmptyDataSource(7);
}
private class SubreportExpression extends AbstractSimpleExpression<JasperReportBuilder> {
private static final long serialVersionUID = 1L;
@Override
public JasperReportBuilder evaluate(ReportParameters reportParameters) {
int masterRowNumber = reportParameters.getReportRowNumber();
JasperReportBuilder report = null;
try {
switch (masterRowNumber) {
case 1:
report = getCaseReport();
break;
case 2:
report = getRequestsReport();
break;
case 3:
report = getObjectiveReport();
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return report;
}
}
private class SubreportDataSourceExpression extends AbstractSimpleExpression<JRDataSource> {
private static final long serialVersionUID = 1L;
@Override
public JRDataSource evaluate(ReportParameters reportParameters) {
int masterRowNumber = reportParameters.getReportRowNumber();
DRDataSource dataSource = null;
try {
switch (masterRowNumber) {
case 1:
dataSource = getCaseDS();
break;
case 2:
dataSource = getRequestsDS();
break;
case 3:
dataSource = getObjectiveDS();
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return dataSource;
}
}
private JasperReportBuilder getCaseReport() {
JasperReportBuilder report = report();
try {
report
.setTemplate(PdfTemplate.reportTemplate2)
.title(cmp.text("CM Details").setStyle(PdfTemplate.bold12LeftStyle));
for (int i = 1; i <= 4; i++) {
report.addColumn(col.column("", "column" + i, type.stringType()));
}
} catch (Exception e) {
e.printStackTrace();
}
return report;
}
private DRDataSource getCaseDS() {
String[] columns = new String[4];
for (int i = 0; i < 4; i++) {
columns[i] = "column" + (i + 1);
}
DRDataSource dataSource = new DRDataSource(columns);
try {
if (c== null) { // c is a instance variable initiated by constructor
c= new CMCase();
}
Object[] values = new Object[4];
values[0] = "ID";
values[1] = c.getOd(); // String
values[2] = "Person In Charge";
values[3] = c.getPicId();
dataSource.add(values);
values[0] = "Customer ID";
values[1] = c.getCustomerId(); // String
values[2] = "Assign Date";
values[3] = c.getAssignDate(); // date already parsed to String from class
dataSource.add(values);
values[0] = "Account Number";
values[1] = c.getAccountNumber(); // String
values[2] = "Counter";
values[3] = Integer.toString(c.getCounter()); // int parsed to String
dataSource.add(values);
} catch (Exception e) {
e.printStackTrace();
}
return dataSource;
}
在调试模式下,错误发生在 when masterRowNumber
is1
本身所以没有给出剩余的方法