I am using Jasper plugin (v 1.5.3) for Grails (v 1.3.7). I have written following code in a method of a Service
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
}
}
I am getting following run-time error when I call the Service method –
Cannot invoke method generateReport() on null object
I have injected jasperService and imported required classes as described in the plugin page. Moreover, when I debug, I noticed that reportDef is being instantiated properly.
Can anyone suggest anything. Appreciate your help.
Michael, thanks for the insight.
To get a better idea about the scenes behind curtain, I downloaded the plugin source code and used directly in my application. Turns out, JasperService class of the plugin uses dataSource bean.
But, dataSource is not being instantiated when dataSource.getConnection() method is called (which, in turn, throwing the null object error). May be this has something to do with Grails itself, not the plugin. So I modified the JasperPrint() method of JasperService class like below and it worked