Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

jasper reports - Subreport variable always return null

I need to put a variable in the master report which value came from a subreport. I followed instructions from the web, but I've always got null. For testing, I put "18" in the expression of subreport variable, but returns null.

Can someone help me? I will put the JRXML below.

I created a variable var1 in master report:

  1. Name: var1
  2. Calculation: System
  3. Reset type: Report
  4. Increment type: None

In the subreport, I created the variable returnValue

  1. Calculation: No Calculation Function
  2. Expression: 18
  3. Increment type: None
  4. Reset type: Report

In the master, I configured the return value:

  1. From Variable: returnValue
  2. To Variable: var1
  3. Calculation Type: Nothing

Following the two xml files


MASTER

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 4.0.2  -->
<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="master" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <variable name="var1" class="java.lang.Integer" calculation="System"/>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="153" y="26" width="100" height="30"/>
                <text><![CDATA[MASTER]]></text>
            </staticText>
        </band>
    </title>
    <summary>
        <band height="246" splitType="Stretch">
            <subreport>
                <reportElement x="80" y="10" width="360" height="50"/>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <returnValue subreportVariable="returnValue" toVariable="var1"/>
                <subreportExpression><![CDATA["sub1.jasper"]]></subreportExpression>
            </subreport>
            <textField>
                <reportElement x="149" y="147" width="100" height="30"/>
                <textFieldExpression><![CDATA[$V{var1}]]></textFieldExpression>
            </textField>
        </band>
    </summary>
</jasperReport>

SUBREPORT

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 4.0.2  -->
<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="sub1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <variable name="returnValue" class="java.lang.Integer">
        <variableExpression><![CDATA[18]]></variableExpression>
    </variable>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="212" y="24" width="100" height="30"/>
                <text><![CDATA[SUB 1]]></text>
            </staticText>
        </band>
    </title>
</jasperReport>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. One problem is that the sub report is never getting executed since its main dataset does not return any row. So change the When No Data Type of sub report from <NULL> to All Sections No Detail -

enter image description here

  1. Secondly the variable returnValue in Sub report have Reset type as None -

enter image description here

  1. Finally the variable var1 rendered using TextField should have Evaluation Time as Report -

enter image description here

Below are updated JRXMLs -

MASTER

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.15.0.final using JasperReports Library version 6.15.0-dd49bfb94918336b8321d5507193f0169ead4e95  -->
<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="master" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d1a326d7-0db4-41f6-9791-6c0abc454731">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <variable name="var1" class="java.lang.Integer" calculation="System"/>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="153" y="26" width="100" height="30" uuid="ccc1c45e-e056-4a8e-8adc-cebefd388bc9"/>
                <text><![CDATA[MASTER]]></text>
            </staticText>
        </band>
    </title>
    <summary>
        <band height="246" splitType="Stretch">
            <subreport>
                <reportElement x="80" y="10" width="360" height="50" uuid="e251bfe7-5e81-4b79-9bc6-5f0e6c1d9555"/>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <returnValue subreportVariable="returnValue" toVariable="var1"/>
                <subreportExpression><![CDATA["sub.jasper"]]></subreportExpression>
            </subreport>
            <textField evaluationTime="Report">
                <reportElement x="149" y="147" width="100" height="30" uuid="24c3289e-f7a5-4137-9254-1fa2605f6741"/>
                <textFieldExpression><![CDATA[$V{var1}]]></textFieldExpression>
            </textField>
        </band>
    </summary>
</jasperReport>

SUBREPORT

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.15.0.final using JasperReports Library version 6.15.0-dd49bfb94918336b8321d5507193f0169ead4e95  -->
<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="sub1" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="97bfa906-b658-45f6-aff8-3bfa505251b3">
    <variable name="returnValue" class="java.lang.Integer" resetType="None">
        <variableExpression><![CDATA[18]]></variableExpression>
    </variable>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="212" y="24" width="100" height="30" uuid="9332a235-2823-4b53-b6f8-0a7c56d58aee"/>
                <text><![CDATA[SUB 1]]></text>
            </staticText>
        </band>
    </title>
</jasperReport>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...