The report design is invalid. Field not found Jasper's Reports

I am trying to create a basic jasper report using JRBeanCollectionDataSource

. There they have a list of objects inside the javabean.

public class Course {

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

      

The student object looks like

public class Student {

    private String name;
    private int id;
}

      

I want to print student information on 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 I get

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

      

I am starting with jasper reports, can anyone tell me what I am doing wrong here. thank

+3


source to share


3 answers


Correct the problem. the name attribute must be defined internally subdataset

. Otherwise it won't work



<subDataset name="dataset1" uuid="09015d96-ad5a-4fed-aa9e-19d25e02e205">
     <field name="name" class="java.lang.String"/>
</subDataset>

      

+1


source


You must define the fields before using it.

In yours jrxml

, you have three fields defined students

in subDataSet id

and students

. But you haven't defined name

and are using it in yours jrxml

and why you are getting this exception.



Try to define name

for example

<field name="name" class="java.lang.String"/>

      

+5


source


Try to create a subheading. I ran your example on 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 object JRBeanCollectionDataSource

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 main xlm 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 one for the "reportStudent_subreport1.jrxml" subreport:

<?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 just one list of students. You can iterate within the main report and print out the course ID while students report to the detail group with minor modifications. Hope this helps get started.

+1


source







All Articles