hina sachdev
hina sachdev

Reputation: 11

Getting null values in report when using JavaBean Data Source

When I tried to generate a report by using JavaBean Data Source and displaying the bean's field values in the table, the PDF report is showing null values.

I have a simple bean (PersonBean) as follows:

public class PersonBean {

    private String Field1;
    private String Field2;

    public String getField1() {
        return Field1;
    }

    public void setField1(String field1) {
        Field1 = field1;
    }

    public String getField2() {
        return Field2;
    }

    public void setField2(String field2) {
        Field2 = field2;
    }
}

The Person class which populate and return the collection of bean is as follows:

import java.util.ArrayList;
import java.util.List;

public class Person {

    public static java.util.List<PersonBean> getReportData() throws Exception {
        List<PersonBean> personBeanList = null;
        try {
            personBeanList = new ArrayList<PersonBean>();

            PersonBean personBean1 = new PersonBean();
            personBean1.setField1("Hina");
            personBean1.setField2("Sachdev");
            personBeanList.add(personBean1);

            PersonBean personBean2 = new PersonBean();
            personBean2.setField1("Swathi");
            personBean2.setField2("Singh");
            personBeanList.add(personBean2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return personBeanList;
    }
}

The PersonReport class which creates report is as follows:

import java.io.FileNotFoundException;
import java.util.*;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

public class PersonReport implements JRDataSource {

    public static void main(String[] args) throws JRException, FileNotFoundException {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            JasperDesign jasperDesign = JRXmlLoader.load("C:/Users/sachdevh/Desktop/reports/personReport.jrxml");
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, 
                    new JRBeanCollectionDataSource(Person.getReportData()));
            JasperExportManager.exportReportToPdfFile(jasperPrint, "./Util/bean.pdf");
            JasperViewer.viewReport(jasperPrint);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object getFieldValue(JRField arg0) throws JRException {
        return null;
    }

    @Override
    public boolean next() throws JRException {
        return false;
    }
}

By using iReport 4.6.0, I designed a report by inserting table component.

The jrxml file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ..>
    <subDataset name="New Dataset 1" uuid="50bff6d9-9b98-444b-9851-bdd37ea570e4">
        <queryString><![CDATA[]]></queryString>
        <field name="field1" class="java.lang.String">
            <fieldDescription><![CDATA[field1]]></fieldDescription>
        </field>
        <field name="field2" class="java.lang.String">
            <fieldDescription><![CDATA[field2]]></fieldDescription>
        </field>
    </subDataset>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\\Users\\sachdevh\\Desktop\\reports\\"]]></defaultValueExpression>
    </parameter>
    <parameter name="parameter1" class="java.lang.String">
        <defaultValueExpression><![CDATA[$F{field1}]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="field1" class="java.lang.String">
        <fieldDescription><![CDATA[field1]]></fieldDescription>
    </field>
    <field name="field2" class="java.lang.String">
        <fieldDescription><![CDATA[field2]]></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">
            <componentElement>
                <reportElement uuid="b3e3e08d-91d9-4e01-ae74-abdfc270551a" key="table" x="0" y="0" width="555"
                               height="61"/>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components"
                          xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="New Dataset 1" uuid="f5f39e4f-4349-4e5d-9736-03f84a7a7617">
                        <dataSourceExpression>
                            <![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Person.getReportData())]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column uuid="9e2a6076-7f96-40c8-86ae-318b521a4f81" width="90">
                        <jr:tableHeader height="30"/>
                        <jr:tableFooter height="30"/>
                        <jr:columnHeader height="30">
                            <staticText>
                                <reportElement uuid="99411482-c1d5-44fa-aef7-9d9f4bf392ec" x="0" y="0" width="90"
                                               height="30"/>
                                <textElement/>
                                <text><![CDATA[field1]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter height="30"/>
                        <jr:detailCell height="20">
                            <textField>
                                <reportElement uuid="74861d9e-48a8-47ce-a477-81d4f497d483" x="0" y="0" width="90"
                                               height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{field1}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column uuid="84c3b078-0114-452e-b3a3-f1c7dcd1b8cb" width="90">
                        <jr:tableHeader height="30"/>
                        <jr:tableFooter height="30"/>
                        <jr:columnHeader height="30">
                            <staticText>
                                <reportElement uuid="cb87f1a0-bbbf-40b2-bb19-292ecf8d9a18" x="0" y="0" width="90"
                                               height="30"/>
                                <textElement/>
                                <text><![CDATA[field2]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter height="30"/>
                        <jr:detailCell height="20">
                            <textField>
                                <reportElement uuid="84fd788f-24e8-4dfa-8c83-ce388c663f45" x="0" y="0" width="90"
                                               height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{field2}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </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 think the datasource expression below

<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Person.getReportData())]]></dataSourceExpression>

Upvotes: 1

Views: 3552

Answers (1)

Alex K
Alex K

Reputation: 22857

You can solve this issue in two steps:

  1. Move table component to the Title band (now it lies in Column Header band) - to prevent occurring of net.sf.jasperreports.engine.JRRuntimeException: Infinite loop creating new page due to column header overflow exception in cases when datasource has a lot of elements;
  2. Change the table's subDataset dataSourceExpression to value below, that is relevant to your Java code:
<datasetRun subDataset="New Dataset 1" uuid="f5f39e4f-4349-4e5d-9736-03f84a7a7617">
    <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
</datasetRun>

Upvotes: 1

Related Questions