javajasper-plugin

Jasper iReport: Error retrieving field value from bean


I am trying to pass list of days from bean to jasper plugin in Netbeans but i get an error. I have followed this guide http://javabeansinjasper.blogspot.cz/2011/01/javabeans-as-datasource-in.html#comment-form

Error:

Error filling print... Error retrieving field value from bean : den 
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : den      
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)      
at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:1358)      
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1259)      
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1235)      
at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1614)      
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:150)      
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:963)      
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:892)      
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:114)      
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:584)      
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:930)      
at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:928)      
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:1423)      
at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:2033)  
Caused by: java.lang.NoSuchMethodException: Unknown property 'den' 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 

Here is a bean:

public class ListDays {

public ListDays(String den) {
    this.den = den;
}

public ListDays() {
}

public String den;

public String getDen() {
    return den;
}

public void setDen(String den) {
    this.den = den;
}

public static Collection getSeznamDnu(){
    List<String> seznamDnu = new ArrayList<>();
    try {
        for (int i = 0; i<LocalDate.now().getMonth().length(LocalDate.now().isLeapYear()) ;i++){
            LocalDate prvniDenMesice = LocalDate.now().withDayOfMonth(1);
            seznamDnu.add(prvniDenMesice.plusDays(i).format(DateTimeFormatter.ofPattern("EE dd")));}
    } catch (Exception e) {
        System.out.println(e);
    }
    return seznamDnu;

}

}

And here is a report:

<?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="null" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0f6a9791-50d4-4aff-9b3f-da0e5f528443">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="den" class="java.lang.String">
        <fieldDescription><![CDATA[den]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement x="113" y="2" width="100" height="20" uuid="6b5fe0c5-60a2-4db6-96d6-cb9d065e17ff"/>
                <text><![CDATA[den]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <textField>
                <reportElement x="113" y="29" width="100" height="20" uuid="f588ecab-7ede-4e2c-80b9-994efcc45dd8"/>
                <textFieldExpression><![CDATA[$F{den}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

Thanks in advance. Been stacked on this for quite some time. Hopefully question is understandable.


Solution

  • Apart from some minor adjustments - creating separated package and class for generating collection and initialization of variable den - main problem was in this line: seznamDnu.add(prvniDenMesice.plusDays(i).format(DateTimeFormatter.ofPattern("EE dd")));}

    I was filling the collection with Strings instead of beans. Code changed to:

    seznamDnu.add(new ListDays(prvniDenMesice.plusDays(i).format(DateTimeFormatter.ofPattern("EE dd"))));}
    

    And everything works fine. While looking at the code now, I also have to note that I didn't choose bean's name really well.