Dynamicaly pass data fro a java object to jasper report and convert to PDF - jasper-reports

I am new in Jasper/ I report. I am trying to create a report that access or gets its data from the java class. I have been trying to use JRBeanDataSource with no luck. I want the report to be dynamic in code without using i-report. I tried designing a report from i report but i cant seem to make the report compile. here is the error
 Compiling to file... E:\Program Files (x86)\Jaspersoft\iReport-5.0.1\ireport\projects\report1.jasper
net.sf.jasperreports.engine.design.JRValidationException: Report design not valid :       1. Parameter not found : name      2. Parameter not found : surname     at net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:271)     at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:153)     at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:240)     at net.sf.jasperreports.engine.JasperCompileManager.compileToFile(JasperCompileManager.java:156)     at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:439)     at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:528)     at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)     at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
Compilation running time: 60!
The report XML is
<?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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" whenResourceMissingType="Empty" uuid="a686c2f8-36e1-48f1-8894-8324c9f5d041">
<property name="ireport.zoom" value="1.4641000000000006"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="24"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement uuid="b1370c0a-6fd1-44ec-af9a-f4edba088c48" x="97" y="21" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="06c14f56-9416-4d14-a6d3-7f5dbfa6d701" x="97" y="59" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$P{surname}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
can some one help with this?

You refer in the report to 2 parameter expressions: $P{name}, $P{surname}.
These have not been defined.
In order to make your report executable you'd need to add definitions for these parameters, e.g.
..
<property name="ireport.y" value="24"/>
<parameter name="name" class="java.lang.String" />
<parameter name="surname" class="java.lang.String" />
<background>
..
If name and surname shall not be filled by parameters but by your datasource you'd need to register them as fields and refer to them as such.
..
<property name="ireport.y" value="24"/>
<field name="name" class="java.lang.String" />
<field name="surname" class="java.lang.String" />
<background>
..
in the text fields you refer to them as $F{name}, $F{surname}

Related

How to pass a parameter from main report to sub report?

I want to pass my parameter value from main report to the sub report.
I have tried many ways. Also I have tried options from this link as well
passing-parameters-from-main-report-to-subreport-to-subreport-in-jasper
Main report
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0 -->
<!-- 2016-01-03T00:04:36 -->
<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="parademo" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4949f116-97b5-453c-8581-61ccb1b3159e">
<parameter name="TITLEPARA" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["hi title"]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement x="180" y="36" width="220" height="30" uuid="4ac55979-2248-45d2-a001-3c82b5fec56b"/>
<textFieldExpression><![CDATA[$P{TITLEPARA}]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="286" splitType="Stretch">
<subreport runToBottom="false">
<reportElement x="0" y="86" width="560" height="74" uuid="eca8556b-2d65-462f-a3d3-ac95ea6e3ebf"/>
<subreportParameter name="TITLEPARA">
<subreportParameterExpression><![CDATA[$P{TITLEPARA}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA["file:/home/rectrix/workspace/jasper/subparam.jrxml"]]></subreportExpression>
</subreport>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Sub-Report
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0 -->
<!-- 2016-01-03T00:04:17 -->
<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="subparam" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b4c019e3-0dc2-425c-b449-908c4fd21bde">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="MainReportDataSet"/>
<parameter name="TITLEPARA" class="java.lang.String" isForPrompting="false"/>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement x="140" y="45" width="100" height="30" uuid="6b9d5038-4163-47ed-a0cd-812f07b27ff0"/>
<textFieldExpression><![CDATA[$P{TITLEPARA}]]></textFieldExpression>
</textField>
</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>
I am able to print "HI TITLE" Using the parameter in my main report's title band.
I want to pass this value to the sub-report using the parameter and print it on the title band of the sub-report.
I want to pass this value using a parameter only, because I want to know how the parameters works so I could use it for my project to pass the values to sub-report through parameters.
You are passing the parameter correctly, this is not your issue.
Using your example files you have these issues.
If the datasource is empty you need attribute whenNoDataType="AllSectionsNoDetail" on jasperReport tag to display something (both in main and subreport).
The subreport can not be in the detail band (since your datasource is empty), move it into another band example title band.
You can not reference a jrxml in subreportExpression, you need to reference a .jasper file (the jrxml when it has been complied). Note: the path need's to be the absolute path to the file (you normally use a parameter for this so that you can pass it in runtime)
see this for more information on path to subreport Subreport not rendering in main report
For testing I suggest you to put another static text element in the subreport, so that you understand if the subreport is displayed or not.

iReport Designer 5.6.0 style encoding does not work in generated pdf

I have created a report template, with a style:
<style name="style1" pdfEncoding="Cp1250"/>
The full xml:
<?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="report3" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="59f4ec24-a6d9-4c6a-b4a9-735007397fbc">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="style1" pdfEncoding="Cp1250"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<staticText>
<reportElement x="59" y="82" width="161" height="20" uuid="d9389d6d-d10f-4376-8282-7b1bd931c360"/>
<text><![CDATA[Árvíztűrő tükörfúrógép]]></text>
</staticText>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
The generated pdf somehow does not include the character encoding. What property/iDesigner option or setting should be set? Or encoding should be set separately in every field/text property?
Adding a style to a template can solve this problem.
On the style check the Pdf Embedded checkbox and select the required Pdf Encoding from the selection list.

jasperReport/iReport subreport shows blank (not displayed)

I am trying to create a sub report(report1_subreport3.jrxml) from the main report(report1.jrxml) using iReport designer (both with empty data source) . The main report detail band contains a static text ("Main Report") and the subreport element and the subreport contains a static text in its corresponding detail band("Sub Report")
But clicking the preview tab of the main report shows only static text "Main Report" not displaying the static text of subreport ("Sub Report") .
I also went through the link http://community.jaspersoft.com/questions/524978/empty-subreport-xml-datasource-ireport but still facing the issue .
Below is the report1.jrxml
<?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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="20036b98-ef64-4dea-8345-b89bcd8f2671">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="68"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["D:\\soapReport\\jasperReport\\"]]></defaultValueExpression>
</parameter>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<staticText>
<reportElement uuid="d49ee6bf-1a13-41bb-9feb-f0fb2ec414f2" x="71" y="28" width="100" height="20"/>
<textElement/>
<text><![CDATA[Main Report]]></text>
</staticText>
<subreport>
<reportElement uuid="364d3c38-a0a1-4fa5-b0bf-12b22314ac5e" x="311" y="14" width="200" height="100"/>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "report1_subreport3.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
and below is the report1_subreport3.jrxml
<?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="report1_subreport3" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="7ca2d973-972c-4c56-8d5e-503b32b4afc6">
<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"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<staticText>
<reportElement uuid="5f488fe4-f9f7-4f12-9c1e-812661026bd7" x="222" y="50" width="100" height="20"/>
<textElement/>
<text><![CDATA[Sub Report]]></text>
</staticText>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Thanks in advance for any help on that ...
Thanks Gopi for the change, but still it is showing the blank in the place of subreport . Below is the main report
<?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="report1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="748f862c-3467-4c8c-89f6-04a571192482">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["D:/soapReport/jasperReport/"]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="71" splitType="Stretch">
<staticText>
<reportElement uuid="17f2cfc8-497a-4676-b83a-acf55bccac04" x="33" y="15" width="100" height="20"/>
<textElement/>
<text><![CDATA[Main Report]]></text>
</staticText>
</band>
</detail>
<columnFooter>
<band height="134" splitType="Stretch">
<subreport>
<reportElement uuid="40f53bea-c9eb-46a7-b4fc-3d592da3ef90" x="234" y="17" width="200" height="100"/>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA["D:/soapReport/jasperReport/report1_subreport1.jasper"]]></subreportExpression>
</subreport>
</band>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
and below is the subreport
<?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="report1_subreport1" language="groovy" pageWidth="555" pageHeight="802" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="62bf7fb7-8145-4aa6-b963-c98eefac9862">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement uuid="9bf9eafc-c1e0-49fa-8bb8-ed6c496498da" x="213" y="20" width="100" height="20"/>
<textElement/>
<text><![CDATA[Sub Report]]></text>
</staticText>
</band>
</columnHeader>
</jasperReport>
PLease help me if I am missing something...
Place your sub-report field in the any other band other than detail band and add whenNoDataType="AllSectionsNoDetail" to your xml file. Add it among the other report properties in <JasperReport> tag. You can also add using the GUI Designer. Open your sub report and in the properties editor, at the end of the options you will find When No Data option. Select All Sections with No Detail from the list of available options.
Mainreport :
<?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="report1" 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="12"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["/home/qualian/Desktop/gopi/prjreport/"]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<subreport>
<reportElement x="33" y="56" width="208" height="36"/>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR}+"report3_subreport1.jasper"]]></subreportExpression>
</subreport>
<staticText>
<reportElement x="33" y="15" width="100" height="20"/>
<textElement/>
<text><![CDATA[Main Report]]></text>
</staticText>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
Sub report:
<?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="report1_subreport3" language="groovy" pageWidth="555" pageHeight="802" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="40" splitType="Stretch">
<staticText>
<reportElement x="107" y="12" width="100" height="20"/>
<textElement/>
<text><![CDATA[Sub Report]]></text>
</staticText>
</band>
</columnHeader>
This worked like a charm. Hope this helps.
Above issue got resolved using below tag which I had included in my mainreport. We need to use empty data source (new net.sf.jasperreports.engine.JREmptyDataSource())
SUBREPORT.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1 -->
<!-- 2016-12-15T10:33:58 -->
<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="HYPERLINKS_SUBREPORT" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="ad9606b1-969d-4340-a7eb-61dc30ec8f0d">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
<queryString>
<![CDATA[]]>
</queryString>
<detail>
<band height="82" splitType="Stretch">
<staticText>
<reportElement x="16" y="47" width="100" height="30" uuid="866e60e7-16a0-4fea-b56f-ca4143e98fec"/>
<text><![CDATA[Testing text]]></text>
</staticText>
</band>
</detail>
XML of main_report.jrxml
<subreport>
<reportElement positionType="Float" x="0" y="220" width="200" height="60" uuid="62463158-73b1-4089-b66c-633d3a987649"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
<subreportExpression><![CDATA["SUBREPORT.jasper"]]></subreportExpression>
</subreport>
The same can be achieved using Jaspersoft Studio.
Anyone having a problem with jasper reports studio 6.3.0 not displaying subreports should try changing the name of the parameter in the subreport and then update the parameter connection in the master reports subreport widget "edit parameter" option to match the new parameter name. Also might avoid underscore ie. "USER_ID".
In my case problem was caused by JasperReportStudio NOT building .jasper file after saving subreport. This caused the the main report to always include very first version of subreport I have created (which had some error, so was not displaying anything).
Solution is to press Ctrl-B after pressing Ctrl-S each time you save. This rebuilds subreport .jasper file and caused it to refresh properly in preview of main report.

Returning a value from Sub Report to Main Report in iReport

Hi every one I am using iReports for generating one of the reports and stuck at one place.
The situation is like this:
I am using one sub report in my main report and i want to return a variable (float) back to the main report from sub report after the query has been executed. And i'm just getting null values back to main report i have wasted 2 days googling and searching but problem is still there..
bellow is my dummy code of my JRXMLS (perfectly same) and snaps...
Main Report JRXML
<?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="anuj" 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"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["/home/anuj/Reports/"]]></defaultValueExpression>
</parameter>
<queryString language="SQL">
<![CDATA[select * from "SensorType"]]>
</queryString>
<field name="SensorTypeId" class="java.lang.Integer"/>
<field name="SensorTypeName" class="java.lang.String"/>
<variable name="A" class="java.lang.Integer" resetType="None" calculation="System"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band splitType="Stretch"/>
</title>
<pageHeader>
<band splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band splitType="Stretch"/>
</columnHeader>
<detail>
<band height="23" splitType="Stretch">
<textField>
<reportElement x="71" y="3" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{SensorTypeId}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="202" y="3" width="112" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{SensorTypeName}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="5" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="1" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch">
<subreport>
<reportElement x="183" y="16" width="257" height="26"/>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<returnValue subreportVariable="A" toVariable="A"/>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "anuj_subreport1.jasper"]]></subreportExpression>
</subreport>
<textField>
<reportElement x="71" y="22" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$V{A}]]></textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>
Sub Report JRXML
<?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="anuj_subreport1" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="149"/>
<queryString>
<![CDATA[select Sum("SensorTypeId") from "SensorType";]]>
</queryString>
<field name="sum" class="java.lang.Long"/>
<variable name="A" class="java.lang.Integer" resetType="None" calculation="System">
<variableExpression><![CDATA[$F{sum}]]></variableExpression>
</variable>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="169" y="59" width="100" height="20"/>
<textElement/>
<text><![CDATA[sum]]></text>
</staticText>
<textField>
<reportElement x="216" y="59" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{sum}]]></textFieldExpression>
</textField>
</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>
Thanks in advance..
Although the question is already answered, I would like to highlight the importance of having calculation="System" on the variable in the Main Report (the report calling the SubReport).
I wasted a lot of time before figuring this out...
In the dummy-code for Main Report JRXML above, the variable "A" correctly has calculation="System". NB: The answer by #GenericJon covers the variable in the SubReport.
In the subreport variable, you have calculation=System. This should be used when you are calculating the variable yourself using a scriptlet. You do not have an appropriate scriptlet attached to the report, so the variable is never being calculated.
You probably want to change the variable definition so that it is calculated at the start of the subreport, something like this:
<variable name="A" class="java.lang.Integer" resetType="Report" calculation="Nothing">
<variableExpression><![CDATA[$F{sum}]]></variableExpression>
</variable>
resetType="Report" will never reset the variable while the report is running.
calculation="Nothing" will evaluate the variableExpression for every row in the dataset.
These attributes could be omitted though, as they are the default values used. You should then find that the variable is initialised correctly.
From the JasperReports Ultimate Guide:
The value coming from the subreport is available only when the whole band containing the subreport is printed. If you need to print this value using a textfield placed in the same band as your subreport, set the evaluation time of the textfield to Band
i am agree with #bubba_hego99, the calculation type as below:
system no caculation, just set as system, or valued as other operator.
nothing no calulation, just set the variable.
sum execute sum($Feild("A")).
....
The detail you want to know other caculation, you should be to see the ireport document.
Note, in earlier versions of JasperReports:
When using a subreport returnValue, the variable returned must not be in the same band as the subreport itself.
The affectation is done I think at the end of the rendering of the band.
So add another band and use your variable inside.

JasperReports collection as datasource?

I'm trying to find out how to use a string collection as a datasource in JasperReports.
I guess I found the solution, but I can't get this to work, can anyone assist me?
set as datasource the special data source JREmptyDatasource( $F{my_collection}.size() )
This creates an empty data set with the same size of records as the collection.
Pass the $F{my_collection} as parameter to the sub dataset (using the data set run of the List).
To print the current element of the collection:
$P{my_collection_param}.get( $V{REPORT_COUNT} )
<?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="report1" 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"/>
<subDataset name="dataset1">
<parameter name="my_collection_param" class="java.util.Collection" isForPrompting="false">
<defaultValueExpression><![CDATA[$P{my_collection}]]></defaultValueExpression>
</parameter>
</subDataset>
<parameter name="my_collection" class="java.util.Collection">
<defaultValueExpression><![CDATA["anna"]]></defaultValueExpression>
</parameter>
<field name="my_collection" class="java.util.Collection">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<componentElement>
<reportElement x="51" y="30" width="400" height="50"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="dataset1">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource($F{my_collection}.size())]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="50" width="0">
<textField>
<reportElement x="159" y="24" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$P{my_collection_param}.get( $V{REPORT_COUNT} )]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
I guess your hack of using JREmptyDataSource is good by definition, if it works.
But you'll find that things are lot easier if you use this instead:
new net.sf.jasperreports.engine.data.JRMapCollectionDataSource($P{my_collection_param})