LaVOZs

The World’s Largest Online Community for Developers

'; java - Jasper reports List as Parameter - LavOzs.Com

I am having a problem with rendering list sent as parameter in a jasperreport. I will have java application send the list filled with data. I want jasper to render the 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="r1_10_002_active_agent_invol_jrxml_1275409438288" pageWidth="1040" pageHeight="792" orientation="Landscape" whenNoDataType="NoDataSection" columnWidth="1032" leftMargin="4" rightMargin="4" topMargin="20" bottomMargin="20" uuid="ac26d847-4888-412d-832a-29f36daa0c69">
    <property name="ireport.scriptlethandling" value="0"/>
    <property name="ireport.encoding" value="UTF-8"/>
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <property name="ireport.jasperserver.reportUnit" value="/reports/Test/Test"/>
    <property name="ireport.jasperserver.url" value="http://localhost:8080/jasperserver/services/repository"/>
    <import value="net.sf.jasperreports.engine.*"/>
    <import value="java.util.*"/>
    <import value="net.sf.jasperreports.engine.data.*"/>
    <style name="column_header" mode="Opaque" forecolor="#000000" backcolor="#F1E5D4" hAlign="Center" vAlign="Bottom" fontName="Arial">
        <box bottomPadding="2">
            <pen lineWidth="0.0" lineStyle="Solid" lineColor="#FFFFFF"/>
            <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#FFFFFF"/>
            <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#FFFFFF"/>
            <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#FFFFFF"/>
            <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#FFFFFF"/>
        </box>
    </style>
    <style name="alternate_row" mode="Opaque" forecolor="#000000" backcolor="#FFFFFF" hAlign="Left" vAlign="Middle" fontName="Arial">
        <box leftPadding="2" rightPadding="2">
            <pen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
        </box>
    </style>
    <style name="title_2" isDefault="true" mode="Opaque" forecolor="#000000" backcolor="#FFFFFF" hAlign="Left" vAlign="Middle" fontName="Arial" fontSize="11" isBold="true">
        <box leftPadding="4" rightPadding="2"/>
    </style>
    <style name="default_text" isDefault="true" mode="Opaque" forecolor="#000000" backcolor="#FFFFFF" hAlign="Left" vAlign="Middle" fontName="Arial">
        <box leftPadding="2" rightPadding="2"/>
    </style>
    <style name="column_header_sorted" mode="Opaque" forecolor="#000000" backcolor="#958671" hAlign="Center" vAlign="Bottom" fontName="Arial" fontSize="11" isBold="false">
        <box bottomPadding="2">
            <pen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
        </box>
    </style>
    <style name="title" mode="Opaque" forecolor="#000000" backcolor="#FFFFFF" hAlign="Left" vAlign="Top" fontName="Arial" fontSize="24" isBold="true">
        <box topPadding="2" leftPadding="4"/>
    </style>
    <style name="group_section" mode="Opaque" forecolor="#000000" backcolor="#D3CAB7" hAlign="Left" vAlign="Middle" fontName="Arial" fontSize="12" isBold="false">
        <box leftPadding="2" rightPadding="2">
            <pen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
        </box>
    </style>
    <style name="group2_section" mode="Opaque" forecolor="#6B542E" backcolor="#FFFFFF" hAlign="Left" vAlign="Middle" fontName="Arial" fontSize="11" isBold="true" isItalic="false">
        <box leftPadding="2" rightPadding="2">
            <pen lineWidth="0.0" lineStyle="Dashed" lineColor="#FFFFFF"/>
            <topPen lineWidth="0.0" lineStyle="Dashed" lineColor="#FFFFFF"/>
            <leftPen lineWidth="0.0" lineStyle="Dashed" lineColor="#FFFFFF"/>
            <bottomPen lineWidth="0.0" lineStyle="Dashed" lineColor="#FFFFFF"/>
            <rightPen lineWidth="0.0" lineStyle="Dashed" lineColor="#FFFFFF"/>
        </box>
    </style>
    <style name="section" mode="Opaque" forecolor="#000000" backcolor="#999999" hAlign="Left" vAlign="Middle" fontName="Arial" fontSize="12">
        <box leftPadding="2" rightPadding="2"/>
    </style>
    <subDataset name="dataset3" uuid="24fb1a54-d444-461e-ac5e-7343f728ee8c">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="company" class="java.lang.String"/>
    </subDataset>
    <parameter name="environment" class="java.lang.String"/>
    <parameter name="username" class="java.lang.String"/>
    <parameter name="show_header" class="java.lang.String"/>
    <parameter name="users" class="java.util.Collection">
        <defaultValueExpression><![CDATA[java.util.Arrays.asList( new vip.User("Agent 1"), new vip.User("Agent 2") )]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[select 1 from dual]]>
    </queryString>
    <field name="id" class="java.lang.Integer"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="58" splitType="Stretch">
            <printWhenExpression><![CDATA[new Boolean($P{show_header}.equals("1"))]]></printWhenExpression>
            <staticText>
                <reportElement uuid="a9d04c94-435d-4baf-9d72-7321d7389aa8" key="staticText-8" style="title" x="194" y="0" width="838" height="33"/>
                <box topPadding="2" leftPadding="4">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <text><![CDATA[Active Agent Involuntary Fee]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="c203c163-7092-44d7-86d9-a74572a5c4f8" key="staticText-16" style="title_2" x="194" y="40" width="112" height="15"/>
                <box leftPadding="4" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <text><![CDATA[Environment: ]]></text>
            </staticText>
            <textField isBlankWhenNull="false">
                <reportElement uuid="e586fc32-d009-4996-97b8-f8931501fe83" key="textField-49" style="default_text" x="306" y="40" width="246" height="15"/>
                <box leftPadding="2" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$P{environment}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="e5313ccd-3c5a-4dfd-a2bc-dc9663f6ca16" key="staticText-17" style="title_2" x="552" y="40" width="112" height="15"/>
                <box leftPadding="4" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <text><![CDATA[User: ]]></text>
            </staticText>
            <textField isBlankWhenNull="false">
                <reportElement uuid="26bb86f6-7bfc-4afc-aee2-6a064197387f" key="textField-50" style="default_text" x="664" y="40" width="251" height="15"/>
                <box leftPadding="2" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$P{username}]]></textFieldExpression>
            </textField>
            <image scaleImage="RetainShape">
                <reportElement uuid="e34dbb4b-ca15-4e88-9f91-657e20fc09c7" key="image-1" style="default_text" x="7" y="6" width="177" height="44"/>
                <imageExpression><![CDATA["repo:/PAR/VIPR/Images/Logo_BW.gif"]]></imageExpression>
            </image>
        </band>
    </title>
    <pageHeader>
        <band height="32" splitType="Stretch">
            <textField isBlankWhenNull="false">
                <reportElement uuid="9d71a482-e52e-4fc8-8455-4bc6d834af73" key="textField-3" style="column_header_sorted" x="552" y="0" width="63" height="20"/>
                <box bottomPadding="2">
                    <pen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center"/>
            </textField>
        </band>
    </pageHeader>
    <detail>
        <band height="200" splitType="Prevent">
            <staticText>
                <reportElement uuid="8d2b2a61-53bc-4d25-be40-e984af9555e5" x="270" y="0" width="200" height="15"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Bean with List sample]]></text>
            </staticText>
            <componentElement>
                <reportElement uuid="c5f0d6ad-400c-4ae8-acdc-92ea0b43a003" x="270" y="0" width="200" height="15"/>
                <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="dataset3" uuid="ec5dd79d-3f64-46d2-b4fa-b6c05abbe86a">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{users})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="15" width="222">
                        <textField>
                            <reportElement uuid="a9e856e2-9749-4d94-9a46-55384fd1b524" x="100" y="0" width="100" height="15"/>
                            <textElement/>
                            <textFieldExpression><![CDATA[$F{company}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </detail>
    <columnFooter>
        <band splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="15" splitType="Stretch">
            <printWhenExpression><![CDATA[new Boolean($P{show_header}.equals("1"))]]></printWhenExpression>
            <textField isBlankWhenNull="false">
                <reportElement uuid="947b6598-b833-41f9-a95b-3496207f0aeb" key="textField-33" style="default_text" x="0" y="0" width="558" height="15"/>
                <box leftPadding="2" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA["Date Printed: " + new Date().toLocaleString()]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="false">
                <reportElement uuid="6575ad0c-1884-4c8d-adef-ed28e0753d42" key="textField-37" style="default_text" x="558" y="0" width="444" height="15"/>
                <box leftPadding="2" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA["Page " + $V{PAGE_NUMBER}.toString() + " of "]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report" isBlankWhenNull="false">
                <reportElement uuid="0d67d248-a477-4c81-8a34-845cb2f7b4e6" key="textField-38" style="default_text" x="1002" y="0" width="30" height="15"/>
                <box leftPadding="2" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

Java class:

package vip;

public class User {

private String company;
private String companyType;
private String eecdId;
private String firstName;
private String lastName;
private String email;
private String userId;
private String status;

public User(String company) {
    super();
    this.company = company;
}
public String getEecdId() {
    return eecdId;
}
public void setEecdId(final String eecdId) {
    this.eecdId = eecdId;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(final String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(final String lastName) {
    this.lastName = lastName;
}
public String getEmail() {
    return email;
}
public void setEmail(final String email) {
    this.email = email;
}
public void setCompany(final String company) {
    this.company = company;
}
public String getCompany() {
    return company;
}
public void setCompanyType(final String companyType) {
    this.companyType = companyType;
}
public String getCompanyType() {
    return companyType;
}
public void setUserId(final String userId) {
    this.userId = userId;
}
public String getUserId() {
    return userId;
}
public void setStatus(final String status) {
    this.status = status;
}
public String getStatus() {
    return status;
}

}

But I am getting below error while trying to access field company in the JRXML.

net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : company      at net.sf.jasperreports.components.list.VerticalFillList.prepare(VerticalFillList.java:139)      at net.sf.jasperreports.engine.fill.JRFillComponentElement.prepare(JRFillComponentElement.java:129)      at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:328)      at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:377)      at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:351)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2039)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:771)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:281)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:144)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:841)      at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:88)      at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:653)      at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:569)      at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:915)      at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:928)      at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)      at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)  Caused by: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : company      at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123)      at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)      at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100)      at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:1317)      at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1218)      at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1194)      at net.sf.jasperreports.components.list.FillDatasetRun.next(FillDatasetRun.java:168)      at net.sf.jasperreports.components.list.VerticalFillList.prepare(VerticalFillList.java:97)      ... 17 more  Caused by: java.lang.NoSuchMethodException: Unknown property 'company' on class 'class java.lang.String'      at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322)      at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)      at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)      at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)      at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)      ... 24 more 

Jasper reports is struggling to find the field from vip.User. It works when I use prompt as false use the defaultValueExpression populated with array list.

user4444282

Why do you need to send list as a parameter ?

If therer is no special reasons. Then should follow this,

Map parameters = new HashMap();
JasperPrint jasperPrint = null;
jasperPrint  = JasperFillManager.fillReport(jasperReport, parameters, new JRBeanCollectionDataSource(yourList));

And also add your User bean class as Java Beans datasource in Jrxml file. For More Click

Related
Java optional parameters
Does Java support default parameter values?
Converting array to list in Java
Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?
Java, 3 dots in parameters
Jasper reports and report parameters
What are the -Xms and -Xmx parameters when starting JVM?
Java 8 List<V> into Map<K, V>