JasperReports accessing to property javabeans

I´m studying jasperreports and I want access to property subclass for example Report.Persona.name but jasperreport show me this error:

net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : name
Caused by: java.lang.NoSuchMethodException: Unknown property 'name' on class 'class com.test.dto.Report'

I´m trying accessing by

but show other errors.

I have 3 DTO class

Person

public class Person {

   private int id;
   private String name;
   private String lastname;
   private String age;
   ...getter setter

Ticket

public class Ticket {

   private int number;
   private String typeTicket;
   private String dateTicket;
   ...getter setter

and this class that group both last class

Report

public class Report {

   private Person person;
   private List<Ticket> ticket;
   ...getter setter

A main class to call my collection(Report) and send to parameter with JRBeanCollectionDataSource

Main

public class Main {

public static void main(String[] args) throws FileNotFoundException, JRException {

    JasperDesign jasperDesign;
    JasperPrint jasperPrint;
    JasperReport jasperReport;

    try
    {
        InputStream base = new FileInputStream(new File("C:\\Users\\Documents\\NetBeansProjects\\TestReport\\src\\jrxml\\Template.jrxml"));

        jasperDesign = JRXmlLoader.load(base);
        //compile 
        jasperReport = JasperCompileManager.compileReport(jasperDesign);

        jasperPrint = JasperFillManager.fillReport(jasperReport, null,
                new JRBeanCollectionDataSource(
                        getMyData()));
        //view the report using JasperViewer
        JasperViewer.viewReport(jasperPrint);
    }
    catch (JRException e)
    {
        e.printStackTrace();
    }


}


private static Collection getMyData()
{

    List<Report> data = new LinkedList<Report>();
    Report report = new Report();

    Person p1 = new Person();
    p1.setName("My Name ");
    p1.setAage("100");
    p1.setLastName("Last Name");

    report.setPerson(p1);

    data.add(reporte);

    // .... comment code to List<Ticket>
    return data;
}

}

Answers


Well, the way Jasper-Reports deals with bean properties, is reading them as parameters. An you just have to create a Map object to store them. For example, inside your Java code (in Main class, as there is the code you use to generate the report) you have to put this snippet :

Map parameters = new HashMap();
String name = Report.person.getName();
parameters.put("name", name);

jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JRBeanCollectionDataSource(
                    getMyData()));

This way, the person's name stored in Report class, will be visible to the jrxml of the report. Now, the second step is to use this parameter in the report, by an expression like:

$P{name}

And that is all you need.


Need Your Help

Restricting speech recognition results on Android

java android speech-recognition

I'm making an app that allows people to speak and select between a few options (Strings). I'm having a little problem making the Android Speech Recognizer fit my idea.

How Google API V 3.0 .Net library and Google OAuth2 Handling refresh token

c# google-api google-calendar-api google-oauth google-api-dotnet-client

In my application I am using Google API V 3.0 .Net library with Google OAuth2 to synchronize Google calender and outlook calender. I am using below code to get the Google.Apis.Calendar.v3.CalendarS...