Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

java - Unable to retrieve value from a JavaBean while generating reports using JasperReports API

I am trying to generate a simple JR report from a list.

I am keep getting Error retrieving field value from bean : name

This error comes due to wrong getter method-name since jasper uses reflection to take the fields from a bean. However even after correcting the getter method-name. I keep getting this exception. Is there any other problem?

My jrxml file is

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jasperReport PUBLIC  "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport name="simpleReport">
  <field name="name" class="java.lang.String"/>
  <field name="count" class="java.lang.String"/>
  <title>
        <band height="50">
            <staticText>
          <reportElement x="0" y="0" width="180" height="15"/>
            <textElement/>
          <text><![CDATA[Report]]></text>
        </staticText>
        </band>
    </title>
  <pageHeader>
      <band/>
  </pageHeader>
  <columnHeader>
        <band height="20">
        <staticText>
          <reportElement x="180" y="0" width="180" height="20"/>
        <textElement>
            <font isUnderline="true"/>
        </textElement>
        <text><![CDATA[Event Name]]></text>
      </staticText>
      <staticText>
        <reportElement x="360" y="0" width="180" height="20"/>
        <textElement>
          <font isUnderline="true"/>
        </textElement>
        <text><![CDATA[Count]]></text>
      </staticText>
    </band>
  </columnHeader>
  <detail>
    <band height="20">
      <textField>
        <reportElement x="180" y="0" width="180" height="15"/>
        <textElement/>
        <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
      </textField>
      <textField>
        <reportElement x="360" y="0" width="180" height="15"/>
        <textElement/>
        <textFieldExpression><![CDATA[$F{count}]]></textFieldExpression>
      </textField>
    </band>
  </detail>
  <columnFooter>
    <band/>
  </columnFooter>
  <pageFooter>
    <band height="15">
      <staticText>
        <reportElement x="0" y="0" width="40" height="15"/>
        <textElement/>
        <text><![CDATA[Page:]]></text>
      </staticText>
      <textField>
        <reportElement x="40" y="0" width="100" height="15"/>
        <textElement/>
        <textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
      </textField>
    </band>
  </pageFooter>
  <summary>
    <band/>
  </summary>
</jasperReport>

Bean class is

class EventBean {
    private String name;
    private String count;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCount() {
        return count;
    }
    public void setCount(String count) {
        this.count = count;
    }
}

class EventNameList {
    public ArrayList<EventBean> getDataBeanList() {
        ArrayList<EventBean> list = new ArrayList<EventBean>();

        list.add(generate("Flow", "100"));
        list.add(generate("Non flow", "300"));
        list.add(generate("Allow", "600"));
        list.add(generate("Deny", "50"));

        return list;
    }

    private EventBean generate(String name, String country) {
        EventBean bean = new EventBean();
        bean.setName(name);
        bean.setCount(country);

        return bean;
    }
}

And I am generating reports here

JasperCompileManager.compileReportToFile(inpuutjrxml, outputjasper);

EventNameList list = new EventNameList();
JRBeanCollectionDataSource beanList = new JRBeanCollectionDataSource(list.getDataBeanList());

JasperPrint jasperPrint = JasperFillManager.fillReport(outputjasper, new HashMap(), beanList);

JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream(pefoutput));

Do we need to make any more modification to bean class?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The solution is very simple - you should change the access modifier of JavaBean class to public.

Like this:

public class EventBean {
    private String name;
    private String count;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }
}

Don't forget that you are using your own package.


You can find more information about JavaBean Data Sources here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...