javajasper-reportsireportsubreport

How to Pass JR Bean Data Source only to subreport and JREmptyData Source to master IReport


I a new to jasper report and i am not getting how to pass JRBeanCollectionDataSource only to subreport and JREmptyDataSource to master ireport.

I have one java bean class usingbean having properties compet and compet_name with getter and setter methods.

In my master report i have one field name as dataBeanList which is the Arraylist of type usingbean and field property is set to type list.

My subreport is having two fields named as compet and compet_name...

   <subreport>
                    <reportElement uuid="0db7b8b7-9cf5-41fc-b271-ffb6333c80f3" x="129" y="30" width="200" height="100"/>
                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{dataBeanList})]]></dataSourceExpression>
                    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subreport1.jasper"]]></subreportExpression>
                </subreport>

My java code is...

   try
        {

             Map<String, Object> parameters = new HashMap<String, Object>();

            newpdftemplate newpdftemplate = new newpdftemplate();
            ArrayList<usingbean> dataBeanList = newpdftemplate.getDataBeanList();

            JRBeanCollectionDataSource beanColDataSource = new
            JRBeanCollectionDataSource(dataBeanList);
jasperDesign = JRXmlLoader.load("D:/workspace/itxtgraph/jrxml/FirstReport.jrxml");
        jasperReport = JasperCompileManager.compileReport(jasperDesign);
parameters.put("SUBREPORT_DIR", "D:/workspace/itxtgraph/jrxml/");
         jasperPrint = JasperFillManager.fillReport(jasperReport,parameters, new JREmptyDataSource());
        JasperExportManager.exportReportToPdfFile(jasperPrint,
        "D:/workspace/itxtgraph/report/SecondReport.pdf");
        response.setContentType("application/pdf");
}

My master report is getting filled but subreport is empty with static texts.. Please tell me where i am doing mistakes

error is:

 13 Nov, 2013 4:44:31 PM net.sf.jasperreports.engine.fill.JRFillSubreport prepare
SEVERE: Fill 1: exception
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : 
    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:1331)
    at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1232)
    at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1208)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1577)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:149)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:932)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:864)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:655)
    at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59)
    at net.sf.jasperreports.engine.fill.AbstractThreadSubreportRunner.run(AbstractThreadSubreportRunner.java:203)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NoSuchMethodException: Unknown property '' on class 'class itxtgraph.usingbean'
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1313)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:762)
    at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837)
    at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
    ... 15 more

why each element of array list is displaying in new page if bean fields are directly taken in main report


Solution

  • You need to pass the subreport datasource to the main report as a parameter rather than a field.

    Java:

    ...
    Map<String, Object> parameters = new HashMap<String, Object>();
    newpdftemplate newpdftemplate = new newpdftemplate();
    ArrayList<usingbean> dataBeanList = newpdftemplate.getDataBeanList();
    JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataBeanList);
    parameters.put("subReportDataSource", beanColDataSource);
    ...
    

    JRXML:

    ...
    <parameter name="subReportDataSource" class="net.sf.jasperreports.engine.JRDataSource"/>
    ...
    <subreport>
        <reportElement uuid="0db7b8b7-9cf5-41fc-b271-ffb6333c80f3" x="129" y="30" width="200" height="100"/>
        <dataSourceExpression><![CDATA[$P{subReportDataSource}]]></dataSourceExpression>
        <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subreport1.jasper"]]></subreportExpression>
    </subreport>
    ...
    

    In your current code, the field dataBeanList is null as you are passing an empty datasource to the main report.