How to alter JFreeChart Scriptlet example to use external data source?

I have used the JFreeChartScriptlet to generate a chart that uses the XYZDataset. For testing I've hardcoded the datasource (an XML file) but I need to hook the scriptlet up the report's datasource which is the content of a webservice response. Due to the number of data points it is impracticle to access the chart data via a number of fields.
The $V{Chart} is in a subreport; the subreport's data source expression is:
In the scriptlet I'm trying to access the data source as follows:
JRXmlDataSource xmlDataSource = (JRXmlDataSource) this.getParameterValue(JRFillParameter.REPORT_DATA_SOURCE);
When I preview the report I get the following exception:
net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: (($P{REPORT_DATA_SOURCE}).subDataSource("//data")
Caused by: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: (($P{REPORT_DATA_SOURCE}).subDataSource("//data")
at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(
at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(
at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(
at net.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(
at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(
at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(
at net.sf.jasperreports.engine.fill.BaseFillHandle$
Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'net.sf.jasperreports.engine.JREmptyDataSource@38c38f20' with class 'net.sf.jasperreports.engine.JREmptyDataSource' to class ''
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(
at JFreeChartReport_1457576688536_715654.evaluate(calculator_JFreeChartReport_1457576688536_715654:304)
at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(
... 14 more
How do I reference an external data source from within a scriptlet?'s picture
Joined: Apr 14 2015 - 11:19pm
Last seen: 1 year 5 months ago

2 Answers:

I am getting the exact same error, as far as I can tell. I don't know if scriptlets are involved though. I have some reports that work fine in iReports but produce errors in JasperSoft Studio. Hope someone can help with this issue.

ibad.rahman's picture
Joined: Feb 3 2016 - 6:35am
Last seen: 6 years 3 months ago


I managed to get my report working eventually (using the settings given in my original query).

I have a report book component for which the data source expression uses net.sf.jasperreports.engine.JREmptyDataSource; I think my scriptlet erroneously referenced this data source (hence the error "Cannot cast object 'net.sf.jasperreports.engine.JREmptyDataSource ..."). 

To fix this I introduced an intermediate sub-report in which I defined the data source I needed to use in the scriptlet, then, eventually, called a sub-report with the appropriate Scriplet class set.

I hope this is useful.


Chris's picture
Joined: Apr 14 2015 - 11:19pm
Last seen: 1 year 5 months ago