Thursday, July 24, 2014

net.sf.dynamicreports.report.exception.DRException: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean

The stack trace of the exception has the following lines.

net.sf.dynamicreports.report.exception.DRException: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean

Caused by: java.lang.NoSuchMethodException: Property '<bean property>' has no getter method in class

The following code throws such an exception.

public class DynamicReportsTest {
         public DynamicReportsTest() {
                  build();
         }

         private void build() {
                   JasperReportBuilder report = DynamicReports.report();
                   report.columns(col.column("Name", "name", DataTypes.stringType()),
                            col.column("Price", "price", DataTypes.doubleType()));

                    report.dataDataSource(createDataSource());
                    report.show();
         }

         private JRBeanCollectionDataSource createDataSource() {
                   List<ReportData> dataSource = new ArrayList<ReportData>();
                   ReportData data = new ReportData();

                   data.setName("Apple");
                   data.setPrice(1.89d);

                   dataSource.add(data);
                   data = new ReportData();    
                  data.setName("Papaya");
                   data.setPrice(2.35d);

                   dataSource.add(data)

                   return new JRBeanCollectionDataSource(dataSource);
          }
     
         private class ReportData {
                   private String name;
                   private double price;

                   public String getName() {
                             return name;
                   }
                   public void setName(String n) {
                            name = n;
                   }
                   public double getPrice() {
                             return price;
                   }
                   public void setPrice(double p) {
                            price = p;
                   }
         }

          public static void main(String[] args) {
                     new DynamicReportsTest();
          }
}

When the program is laying out the report, the data source is accessed by other code to retrieve the data. Since the ReportData class is private, its methods is invisible to other code.

To fix the exception simply change the ReportData inner class to public.

           public class ReportData{
                      . . . . . .
           }
       
----------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

2 comments: