1
votes

I have already worked with Jasper Reports using JRResultSetDataSource, but it's the first time I working with Jasper using JRBeanCollectionDataSource.

I'm trying to run a report test with the following code:

Cliente.java

public class Cliente {
    private String nomeCliente;

    public String getNomeCliente() {
        return nomeCliente;
    }

    public void setNomeCliente(String nomeCliente) {
        this.nomeCliente = nomeCliente;
    }
}

Report.java

public class Report<T> {
    private Collection<T> dataList;
    private String jrxml;

    public Report() {
    }

    public Report(Collection<T> dataList, String jrxml) {
        super();
        this.dataList = dataList;
        this.jrxml = jrxml;
    }

    public Collection<T> getDataList() {
        return dataList;
    }

    public void setDataList(Collection<T> dataList) {
        this.dataList = dataList;
    }

    public String getJrxml() {
        return jrxml;
    }

    public void setJrxml(String jrxml) {
        this.jrxml = jrxml;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void getReport(Collection<T> dataList, String jrxml) throws Exception {
        JasperReport report = null;
        JasperDesign reportDesign;

        try {
            reportDesign = JRXmlLoader.load(jrxml);
            report = JasperCompileManager.compileReport(reportDesign);

        } catch (JRException e2) {
            e2.printStackTrace();
        }

        JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);

        Map params = new HashMap();
        params.put("dataSource", dataList);

        JasperPrint jsPrint = null;

        try {
            jsPrint = JasperFillManager.fillReport(report, params, beanColDataSource);
        } catch (JRException e1) {
            e1.printStackTrace();
        }

        JasperViewer viewer = new JasperViewer(jsPrint, false);
        viewer.setVisible(true);
    }
}

clientereport.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="estiloinstitutobeleza_report" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="05a86d77-8b2c-49cc-9bf6-27fbf7a545d5">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Dataset1" uuid="d50d967c-8a6d-4e1e-b44e-49261df940e9">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="nomeCliente" class="java.lang.String"/>
    </subDataset>
    <parameter name="dataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <field name="id" class="java.lang.Integer"/>
    <field name="nome" class="java.lang.String"/>
    <field name="sexo" class="java.lang.Integer"/>
    <field name="datanascimento" class="java.lang.String"/>
    <field name="logradouro" class="java.lang.String"/>
    <field name="numero" class="java.lang.Integer"/>
    <field name="complemento" class="java.lang.String"/>
    <field name="bairro" class="java.lang.String"/>
    <field name="cidade" class="java.lang.String"/>
    <field name="uf" class="java.lang.Integer"/>
    <field name="cep" class="java.lang.String"/>
    <field name="telefone" class="java.lang.String"/>
    <field name="celular1" class="java.lang.String"/>
    <field name="celular2" class="java.lang.String"/>
    <field name="email1" class="java.lang.String"/>
    <field name="email2" class="java.lang.String"/>
    <group name="nome">
        <groupExpression><![CDATA[$F{nomeCliente}]]></groupExpression>
    </group>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="23" splitType="Stretch">
            <staticText>
                <reportElement x="202" y="0" width="167" height="23" uuid="6ec26fa8-d021-455c-8bf5-b2716a0c7919"/>
                <textElement>
                    <font size="16" isBold="true"/>
                </textElement>
                <text><![CDATA[Relatório de Clientes]]></text>
            </staticText>
            <image>
                <reportElement x="1" y="-20" width="172" height="43" uuid="d382676c-4568-4b4f-b9a6-6e4741aab5ee"/>
                <imageExpression><![CDATA["images/EstiloInstitutoBelezaLogo.png"]]></imageExpression>
            </image>
        </band>
    </title>
    <pageHeader>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="435" y="0" width="80" height="20" uuid="06d08240-2804-45f3-8c68-199839537286"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA["Página "+$V{PAGE_NUMBER}+" de"]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="515" y="0" width="40" height="20" uuid="9db45469-0ec8-4a69-8e02-95ecd71639ce"/>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="14" splitType="Stretch">
            <line>
                <reportElement x="0" y="13" width="555" height="1" uuid="3d9ff8e2-9dfa-4e1a-bf04-4c08222532aa"/>
            </line>
            <line>
                <reportElement x="198" y="0" width="1" height="13" uuid="3ccdd4cc-2dcc-4547-981a-7dd0c0795ef3"/>
            </line>
            <line>
                <reportElement x="300" y="0" width="1" height="13" uuid="2c50fa85-487d-4595-9144-f78adb6d8f32"/>
            </line>
            <line>
                <reportElement x="398" y="0" width="1" height="13" uuid="856b71be-60fd-450f-933e-b092ef412abb"/>
            </line>
            <staticText>
                <reportElement x="0" y="0" width="202" height="14" uuid="36ac836a-63d7-47b5-82b1-c2fb15034dc6"/>
                <text><![CDATA[Nome]]></text>
            </staticText>
            <staticText>
                <reportElement x="202" y="0" width="100" height="14" uuid="8ff1d64c-ae30-4c97-a544-c6eb1989ce05"/>
                <text><![CDATA[Data de Nascimento]]></text>
            </staticText>
            <staticText>
                <reportElement x="402" y="0" width="100" height="14" uuid="01153fec-09ae-4945-b3cb-04c254fb0e65"/>
                <text><![CDATA[Celular]]></text>
            </staticText>
            <staticText>
                <reportElement x="302" y="0" width="100" height="14" uuid="5ab92c2e-f72c-442a-b321-d84841953aa3"/>
                <text><![CDATA[Telefone]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="205" splitType="Stretch">
            <line>
                <reportElement x="0" y="13" width="555" height="1" uuid="ab35d598-27d4-4410-a86a-0876cf839aef"/>
            </line>
            <line>
                <reportElement x="198" y="0" width="1" height="13" uuid="1dd11b36-1226-4917-a2b5-ae60c5c3ef8b"/>
            </line>
            <line>
                <reportElement x="300" y="0" width="1" height="13" uuid="abef3282-f707-4dd8-bd23-a0a07ef60fb6"/>
            </line>
            <line>
                <reportElement x="398" y="0" width="1" height="13" uuid="4b153f11-2e81-4f19-97d3-64f40cda5ffb"/>
            </line>
            <componentElement>
                <reportElement x="1" y="0" width="554" height="13" uuid="21f104d0-3d32-4bf7-a5bf-a062dcb67f89">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    <property name="com.jaspersoft.studio.table.style.table_header" value="Table 1_TH"/>
                    <property name="com.jaspersoft.studio.table.style.column_header" value="Table 1_CH"/>
                    <property name="com.jaspersoft.studio.table.style.detail" value="Table 1_TD"/>
                </reportElement>
                <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="Dataset1" uuid="ec5065e1-b31a-4ae5-8ad9-574969eadcfb">
                        <datasetParameter name="REPORT_DATA_SOURCE">
                            <datasetParameterExpression><![CDATA[$P{dataSource}]]></datasetParameterExpression>
                        </datasetParameter>
                    </datasetRun>
                    <jr:column width="554" uuid="c5e7b9ad-85cf-40fe-aa2d-8f8af92bab6f">
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="554" height="30" uuid="ed6e2650-5949-404b-a542-05e79b134580"/>
                                <textFieldExpression><![CDATA[$F{nomeCliente}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
    <columnFooter>
        <band splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="20" splitType="Stretch">
            <textField evaluationTime="Report">
                <reportElement x="515" y="0" width="40" height="20" uuid="e564df6b-0f6f-4ea1-8a9d-aac36c0a1914"/>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="435" y="0" width="80" height="20" uuid="9cb91f0b-9fb2-475e-a59b-706eb9beccef"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA["Página "+$V{PAGE_NUMBER}+" de"]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

Main.java

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

import br.com.cts.model.Cliente;
import br.com.cts.util.Report;

public class Main {

    public static void main(String[] args) throws Exception {
        Cliente cliente1 = new Cliente();
        cliente1.setNomeCliente("John");

        Cliente cliente2 = new Cliente();
        cliente2.setNomeCliente("Mike");

        List<Cliente> clientes = new ArrayList<>();
        clientes.add(cliente1);
        clientes.add(cliente2);

        Report<Cliente> report = new Report<Cliente>();
        report.getReport(clientes, "reports/clientereport.jrxml");
    }
}

I'm getting the following error:

net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 
     1. Field not found : nomeCliente
    at net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:280)
    at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:152)
    at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:357)
    at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:617)
    at br.com.cts.util.Report.getReport(Report.java:53)
    at br.com.cts.main.Main.main(Main.java:23)
Exception in thread "main" java.lang.NullPointerException
    at net.sf.jasperreports.engine.fill.JRFiller.createReportFiller(JRFiller.java:267)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:156)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:145)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:689)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:1005)
    at br.com.cts.util.Report.getReport(Report.java:68)
    at br.com.cts.main.Main.main(Main.java:23)

I don't know what is wrong at my code.

1
You should add nomeCliente field to nomeClienteAlex K
@AlexK, thanks for your response.Is there no other way to resolve this that I don't need to include an attribute in my string attribute?Daniel Santana
You pass dataList as main collection of your report, but I see that the root fields of the report contains a lot of fields. Where do they come from ? The Client class ? To what kind of record corresponds a detail band of your report ? What is the relation between this object and the nomeClient field ? What is the expression to fill the Dataset1 ? In your jrxml code, it's empty (<![CDATA[]]>).matthiasbe
Sorry for all the questions :pmatthiasbe

1 Answers

1
votes

It looks like you are trying to add an extra layer that does not belong. You are passing to the report a data source that is List<Cliente>. Therefore, your report shouldn't have a subDataset.

Your main report should have

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

It should not have all the other field names that you have unless they are also part of Cliente.

So you are getting the error because the field name is not in the list of fields, just like the error suggests. One reason to have a subDataset would be if you have another List within your main data source (another property in Cliente that is List<String> nicknames or something like that).