Question

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;
}

}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top