Grails的Jasper插件:无法在null对象上调用方法generateReport()
我正在为Grails(V1.3.7)使用Jasper插件(V1.5.3)。我已经在服务的方法中编写了以下代码Grails的Jasper插件:无法在null对象上调用方法generateReport(),grails,jasper-reports,Grails,Jasper Reports,我正在为Grails(V1.3.7)使用Jasper插件(V1.5.3)。我已经在服务的方法中编写了以下代码 import org.codehaus.groovy.grails.plugins.jasper.JasperExportFormat; import org.codehaus.groovy.grails.plugins.jasper.JasperReportDef; import org.apache.commons.io.FileUtils; class ReportService
import org.codehaus.groovy.grails.plugins.jasper.JasperExportFormat;
import org.codehaus.groovy.grails.plugins.jasper.JasperReportDef;
import org.apache.commons.io.FileUtils;
class ReportService {
static transactional = true
def jasperService
def generateFormA() {
def reportDef = new JasperReportDef(name:'test.jasper', fileFormat:JasperExportFormat.PDF_FORMAT)
FileUtils.writeByteArrayToFile(new File('C:/test.pdf'), jasperService.generateReport(reportDef).toByteArray())
return
}
}
调用服务方法时出现以下运行时错误-
无法对null对象调用方法generateReport()
我已经注入了jasperService并导入了页面中描述的必需类。此外,在调试时,我注意到reportDef被正确实例化
有人能提出什么建议吗。感谢您的帮助。您的语法看起来不错,应该可以使用。我会尝试停止应用程序并运行grails命令。有时候这会有帮助。迈克尔,谢谢你的洞察力 为了更好地了解幕后场景,我下载了插件源代码并直接在我的应用程序中使用。事实证明,JasperService插件类使用了数据源bean
class JasperService {
boolean transactional = true
javax.sql.DataSource dataSource
.....
private JasperPrint generatePrinter(JasperReportDef reportDef) {
.....
def conn = dataSource.getConnection()
jasperPrint = JasperFillManager.fillReport(resource.inputStream, reportDef.parameters, conn)
.....
}
...........
}
但是,在调用dataSource.getConnection()方法时,dataSource没有被实例化(这反过来会抛出null对象错误)。这可能与Grails本身有关,而不是与插件有关。因此,我修改了JasperService类的JasperPrint()方法,如下所示,它成功了
def dataSource = org.codehaus.groovy.grails.commons.ApplicationHolder.application.mainContext.dataSource
def conn = dataSource.getConnection()
你“注射”jasperService是什么意思grails会为您做这件事。你能告诉我它的定义在哪里吗?嗨,迈克尔,我已经编辑了我的原始帖子,包括了完整的服务。我调用
generateFormA()
方法