5

Im trying to create a basic jasper report with JRBeanCollectionDataSource. In there im having a list of objects inside the javabean.

public class Course {

    private int id;
    private List<Student> students;
}

Student object looks like

public class Student {

    private String name;
    private int id;
}

I want to print student information inside my report. This is how my jrxml looks like

 <subDataset name="dataset1" uuid="09015d96-ad5a-4fed-aa9e-19d25e02e205">
 <field name="students" class="java.util.List">
  <fieldDescription><![CDATA[students]]></fieldDescription>
 </field>
</subDataset>

<field name="id" class="java.lang.Integer"/>
<field name="students" class="java.util.List"/>
<field name="name" class="java.lang.String"/>

<componentElement>
                <reportElement x="200" y="0" width="400" height="20"/>
                <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.data.JRBeanCollectionDataSource($F{students})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="20" width="400">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="20"/>
                            <box leftPadding="10">
                                <topPen lineWidth="1.0"/>
                                <leftPen lineWidth="1.0"/>
                                <bottomPen lineWidth="1.0"/>
                                <rightPen lineWidth="1.0"/>
                            </box>
                            <textElement/>
                            <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                        </textField>

                    </jr:listContents>
                </jr:list>
            </componentElement>

But when i run this im getting

net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 
     1. Field not found : name
Report design not valid : 
     1. Field not found : name

Im a beginner to jasper reports can anyone please tell me what am i doing wrong here. Thanks

Dilantha
  • 1,552
  • 2
  • 30
  • 46

3 Answers3

10

You have to define the fields before using it.

In your jrxml, you have three field defined students in the subDataSet, id and students. But you haven't defined name and using it in your jrxml and that's why you are getting this exception.

Try defining name, like

<field name="name" class="java.lang.String"/>
MKB
  • 7,587
  • 9
  • 45
  • 71
  • Thanks for the reply. I have defined the name attribute in jrxml. still getting the same error :( – Dilantha Jun 21 '15 at 06:22
  • Thanks for your advice! Just for completeness: In iReport you also need to add fields - you will find them in Report inspector. – Firzen Apr 11 '16 at 11:20
1

Try with a subreport. I launched your example in my local database without any problem.

This is my list of student objects:

private List<Student> getList(){
    List<Student> students = new ArrayList<Student>();
    Connection con = ....; //Retrieve your connection the way you want
    ResultSet rs = con.createStatement().executeQuery("select name, id from students order by id");
    while (rs.next()){
        students.add(new Student(rs.getString(1), rs.getInt(2)));
    }
    con.close();
    return students;
}

This is how I passed my JRBeanCollectionDataSource object from my Java program:

Map<String, Object> params = new HashMap<String, Object>();
params.put("SUBREPORT_DATASOURCE", new JRBeanCollectionDataSource(getList()));

String jasperPath = "....."; //where you have your compiled jasper report file
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath, params, new JREmptyDataSource(1));

This is the xlm main report "reportStudent.jrxml":

    <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="reportStudent" 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_DATASOURCE" class="net.sf.jasperreports.engine.JRDataSource" isForPrompting="false">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="125" splitType="Stretch">
            <subreport>
                <reportElement x="0" y="0" width="555" height="125"/>
                <dataSourceExpression><![CDATA[$P{SUBREPORT_DATASOURCE}]]></dataSourceExpression>
                <subreportExpression><![CDATA["./reportStudent_subreport1.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

And this is the one for the subreport "reportStudent_subreport1.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="reportStudent_subreport1" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <property name="ireport.zoom" value="1.771561000000001"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <field name="id" class="java.lang.String"/>
    <field name="name" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement x="66" y="0" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[id]]></text>
            </staticText>
            <staticText>
                <reportElement x="212" y="0" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[name]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="66" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="212" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

This is for just one list of students. You can iterate inside the main report and print your course id and students subreport in the detail band with just a little changes. Hope it helps to start with.

Turbero
  • 116
  • 3
  • Is it possible to do this without using a subreport ? – Dilantha Jun 21 '15 at 06:23
  • You could insert a table or crosstab in the detail section and print your student list. You will have to configure the necessary parameters to send them to that table/crosstab. Think that detail band is printed once an object in your JRBeanCollectionDataSource (a Course in your case) – Turbero Jun 21 '15 at 19:30
1

Fount out the issue. name attribute should defined inside the subdataset. Otherwise it wont work

<subDataset name="dataset1" uuid="09015d96-ad5a-4fed-aa9e-19d25e02e205">
     <field name="name" class="java.lang.String"/>
</subDataset>
Dilantha
  • 1,552
  • 2
  • 30
  • 46