Java 使用 JasperReports API 从 bean 异常检索字段值时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20517912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:29:56  来源:igfitidea点击:

Getting Error retrieving field value from bean Exception with JasperReports API

javajasper-reportsdynamic-reports

提问by Sachin Verma

I am trying to get a sample report generated by JasperReportsbut it is throwing some exception that I can not understand.

我正在尝试获取JasperReports生成的示例报告,但它抛出了一些我无法理解的异常。

I have a bean:

我有一个豆子:

class DataBean {

    public String country;
    public String name;

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getName() {
        return name;
    }

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

A class that results list of above bean:

产生上述 bean 列表的类:

class DataBeanList {
    public ArrayList<DataBean> getDataBeanList() {
        ArrayList<DataBean> dataBeanList = new ArrayList<DataBean>();
        dataBeanList.add(produce("Manisha", "India"));
        dataBeanList.add(produce("Dennis Ritchie", "USA"));
        dataBeanList.add(produce("V.Anand", "India"));
        dataBeanList.add(produce("Shrinath", "California"));
        return dataBeanList;
    }

    private DataBean produce(String name, String country) {
        DataBean dataBean = new DataBean();
        dataBean.setName(name);
        dataBean.setCountry(country);
        return dataBean;
    }
}

This is how i am doing:

这就是我的做法:

public static void main(String[] args) throws Exception {
    String sourceFileName = "/home/oodles/Samples/jasper_report_template.jasper";
    DataBeanList DataBeanList = new DataBeanList();
    ArrayList<DataBean> dataList = DataBeanList.getDataBeanList();
    System.out.println("<<<" + dataList.get(0).getCountry());
    JasperReportBuilder report = DynamicReports.report();
    JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList, false);
    report.setDataSource(beanColDataSource);
    Map parameters = new HashMap();
    try {
        JasperFillManager.fillReportToFile(sourceFileName, parameters,
                beanColDataSource);
    } catch (JRException e) {
        e.printStackTrace();
    }
}

The Exception is the following:

异常如下:

    net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : country
    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:100)
    at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:1331)
    at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1232)
    at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1208)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1554)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:149)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:841)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:88)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:653)
    at net.sf.jasperreports.engine.JasperFillManager.fillToFile(JasperFillManager.java:542)
    at net.sf.jasperreports.engine.JasperFillManager.fillToFile(JasperFillManager.java:494)
    at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:874)
    at com.general.ReportsMain.main(ReportsMain.java:80)
Caused by: java.lang.NoSuchMethodException: Property 'country' has no getter method in class 'class com.general.DataBean'
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1318)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:762)
    at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837)
    at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
    ... 15 more

In case you need to see .jasper file source:
i am attaching only fields to show only meaningful code here:

如果您需要查看 .jasper 文件源:
我仅附加字段以在此处仅显示有意义的代码:

<field name="country" class="java.lang.String">
    <fieldDescription><![CDATA[country]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
    <fieldDescription><![CDATA[name]]></fieldDescription>
</field>

采纳答案by Sachin Verma

Finally i figured out what you need to do to avoid any exception:

最后我想出了你需要做什么来避免任何异常:

Your datasource is accessed in other packages to extract data from them and if the bean of your datasource is not public then it's not possible to access the class.
SO you need to make your class public (the bean class).

您的数据源在其他包中被访问以从中提取数据,如果您的数据源的 bean 不是公共的,则无法访问该类。
所以你需要公开你的班级(bean 班级)。