JasperReport iReport: JRException: Error retrievin

2019-09-11 02:23发布

问题:

I'm trying to understand how iReport works and how to create a simple jrxml file. The report has a title and header and the data should be in a table. When I try to check how the report looks like using the preview button, I could see the title and header but not the table (Please note that I use that I clicked the "Use default value" for all fields). When I did not use the default value, I get this error.

Error filling print... net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : name net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : name      at net.sf.jasperreports.engine.fill.JRFillSubreport.prepare(JRFillSubreport.java:809)      at net.sf.jasperreports.components.table.fill.FillTableSubreport.prepareSubreport(FillTableSubreport.java:156)      at net.sf.jasperreports.components.table.fill.FillTable.prepare(FillTable.java:400)      at net.sf.jasperreports.engine.fill.JRFillComponentElement.prepare(JRFillComponentElement.java:151)  Caused by: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : name      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:109)  Caused by: java.lang.NoSuchMethodException: Unknown property 'name' on class 'class java.lang.String'      at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322)      at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)      at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)      at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)      at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)      ... 15 more  Print not filled. Try to use an EmptyDataSource...

I thought that maybe that problem has something to do with how I typed the data for List. Probably the format is wrong. This is how I typed the sample data

(myName, 5, Manila)

(Based on the screenshot, Is the format correct? I even tried enclosing it in a square bracket but I still get the same error.)

please note that I have 3 columns Name , Age , Address.

But in the error message I noticed this

Unknown property 'name' on class 'class java.lang.String' 

which makes me think that I did something wrong when I create the dataset. (Sorry if I used a wrong term).

Anyway, here's the xml generated by iReport.

<?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="firstReport" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="597c0716-df6b-42ec-a7c8-863eb1b7174a">
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#7FFFD4">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#BFFFEA">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_TH" mode="Opaque" backcolor="#7FFFD4">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_CH" mode="Opaque" backcolor="#BFFFEA">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Table Dataset 2" uuid="208e382c-09df-484e-8d2d-a4eb4f4e48ba">
        <field name="name" class="java.lang.String"/>
        <field name="age" class="java.lang.String"/>
        <field name="address" class="java.lang.String"/>
    </subDataset>
    <parameter name="companyName" class="java.lang.String"/>
    <parameter name="date" class="java.util.Date"/>
    <parameter name="bean" class="java.util.List"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="32" splitType="Stretch">
            <staticText>
                <reportElement x="182" y="0" width="184" height="25" uuid="fe64d4b4-efb1-4ed2-ae06-772728fc8b65"/>
                <textElement>
                    <font fontName="Bookman Old Style" size="15" isBold="true"/>
                </textElement>
                <text><![CDATA[Chart Of Account List]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="58" splitType="Stretch">
            <staticText>
                <reportElement x="13" y="0" width="100" height="20" uuid="52ae4669-b47e-408c-aabc-e8f470a1d92b"/>
                <text><![CDATA[Company]]></text>
            </staticText>
            <staticText>
                <reportElement x="13" y="38" width="100" height="20" uuid="a27fe0e1-9fe6-423f-a006-46c51c7e3e5e"/>
                <text><![CDATA[Date Exported]]></text>
            </staticText>
            <textField>
                <reportElement x="113" y="0" width="100" height="20" uuid="1d600fa2-9de4-4999-9222-98a8bcb484f9"/>
                <textFieldExpression><![CDATA[$P{companyName}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="113" y="38" width="100" height="20" uuid="1147b818-90ae-4b41-a757-19a590db41d7"/>
                <textFieldExpression><![CDATA[$P{date}]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <componentElement>
                <reportElement key="table 1" style="table 1" x="13" y="0" width="270" height="50" uuid="652523bf-b666-47bc-a1aa-fc20fb9e489a"/>
                <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="Table Dataset 2" uuid="6fcb9950-2d3f-4752-b56d-df7bff1e6991">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{bean})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="90" uuid="ee62967c-3669-49fe-a70d-a588ae47439d">
                        <jr:columnHeader style="table 1_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="90" height="30" uuid="a0973bfe-815a-498e-8b25-c3320cfe0f62"/>
                                <text><![CDATA[Name]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table 1_TD" height="20" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="20" uuid="61d8309e-dd7d-4978-b171-9ae9144e2f5c"/>
                                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="90" uuid="76a9e313-d134-420e-898b-7f5f444c4efb">
                        <jr:columnHeader style="table 1_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="90" height="30" uuid="f79fdf71-a673-4d3a-8e10-b986d99b8e9b"/>
                                <text><![CDATA[Age]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table 1_TD" height="20" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="20" uuid="e4342bed-af9c-4541-9e57-a24485d0a94d"/>
                                <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="90" uuid="5c38c250-d74e-440c-93cb-3fef8ad38157">
                        <jr:columnHeader style="table 1_CH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="90" height="30" uuid="4656a074-f345-483a-971b-b00c95746ec0"/>
                                <text><![CDATA[Address]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table 1_TD" height="20" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="20" uuid="f2cc3d1c-97ef-48c1-b6e4-a12acc4e67ad"/>
                                <textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

1) What is wrong with the jrxml that I made? Did I supply a wrong formatted data to the table? Or there's just something wrong with how I did the report?

回答1:

In parameter $P{bean}, you need to pass a List of bean's where every bean have the methods below (seems that in the List that you are passing there is a String?)

EDIT: with updated post your are passing String's, that does not have these methods.

    //<field name="name" class="java.lang.String"/>
    public String getName() {
       return this.name;
    }

    //<field name="age" class="java.lang.String"/>
    public String getAge() {
      return this.age; //age is a String
    }

    //<field name="address" class="java.lang.String"/>
    public String getAddress() {
      return this.address;
    }

Check that you don't have errors when evaluating them..

EDIT: OP using groovy

In groovy you do not actually need to create the getter's but you still need the bean class es.

class Person {
  String name
  String age
  String address
}


回答2:

This was fixed by adding "new" keyword in the datasource expression. You will see datasource expression by right clicking table in the Report Inspector. "edit Table Datasource"