Hot questions for Using JasperReports in javabeans

Question:

I want to create CSV with Jasper Reports. There are many related questions, but nothing is working for me. Below is my bean class:

public class DataBean {

    public String First_name;
    public String Last_name;
    public String Designation;
    public String Department;

    public void setfirstName(String First_name) {
        this.First_name = First_name;
    }

    public String getfirstName() {
        return First_name;
    } 

    public void setLastName(String Last_name) {
        this.Last_name = Last_name;
    }
    public String getlastName() {
        return Last_name;
   }
    public void setdesignation(String Designation) {
        this.Designation = Designation;
    }
    public String getdesignation() {
       return Designation;
    }
    public void setDepartment(String Department) {
        this.Department = Department;
    }
    public String getDepartment() {
        return Department;
    }
}

Below is class which get data from database and generate CSV with Jasper Report:

import java.util.HashMap;
import java.util.Map;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRCsvExporterParameter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

import com.mysql.jdbc.PreparedStatement;


public class GetResult 
{
    PreparedStatement pst=null;
    DBConnection dbcon=null;
    String First_name;
    String Last_name;
    String Designation;
    String Department;
    net.sf.jasperreports.engine.JasperReport report = null;
    JasperDesign design = null;
    JasperPrint print = null;
    InputStream input = null;
    public void result(){
        try{
            dbcon=new DBConnection();
            input = new FileInputStream(new File("C:/Users/adiuser1/report2.jrxml"));//report2.jasper
           design = JRXmlLoader.load(input);
            ArrayList<DataBean> al2=new ArrayList<DataBean>();
            Map<String, Object> parameters = new HashMap<String, Object>();
            pst = (PreparedStatement) dbcon.conn.prepareStatement("SELECT First_name, Last_name,     Designation, Department FROM emp_data");
            java.sql.ResultSet rs=pst.executeQuery();
            parameters.put("First_name","First_name");
            parameters.put("Last_name","Last_name");
           parameters.put("Designation","Designation");
           parameters.put("Department","Department");
           while(rs.next())
           {
                DataBean db=new DataBean();
                First_name = rs.getString("First_name");
                db.setfirstName(First_name);    
                Last_name = rs.getString("Last_name");
                db.setLastName(Last_name);
                Designation = rs.getString("Designation");
                db.setdesignation(Designation);
                Department = rs.getString("Department");
                db.setDepartment(Department);
                al2.add(db);
            }
            System.out.println("size: "+parameters.size());
            System.out.println("size: "+al2.size());
            report = JasperCompileManager.compileReport(design);
            JRBeanCollectionDataSource masterDS = null;
            masterDS = new JRBeanCollectionDataSource(al2,false);
            print = JasperFillManager.fillReport(report, parameters, masterDS);
            JRCsvExporter csvExporter = new JRCsvExporter();
            csvExporter.setParameter(JRCsvExporterParameter.JASPER_PRINT, print);
            csvExporter.setParameter(JRCsvExporterParameter.OUTPUT_FILE_NAME, "X:/abc.csv");
            csvExporter.setParameter(JRCsvExporterParameter.CHARACTER_ENCODING, "ISO-8859-1");
            csvExporter.exportReport();
       }
        catch(JRException jrException)
        {
            System.out.print(jrException);
            jrException.printStackTrace();
        }
       catch(Exception e)
       {
           System.out.println(e);
      }
      }
   public static void main(String ...s)
   {
        GetResult gr=new GetResult();
       gr.result();
  }
}

Below is my jrxml :

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true" uuid="204c18b7-de66-4171-b8b8-82a20b837f28">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<subDataset name="sum" uuid="73ad5db5-041e-4e27-aed3-acaf6deec1b8">
    <queryString language="SQL">
        <![CDATA[]]>
    </queryString>
    <field name="First_name" class="java.lang.String"/>
    <field name="Last_name" class="java.lang.String"/>
    <field name="Designation" class="java.lang.String"/>
    <field name="Department" class="java.lang.String"/>
</subDataset>
<field name="First_name" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="Last_name" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="Designation" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="Department" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<background>
    <band splitType="Stretch"/>
</background>
<columnHeader>
    <band height="35" splitType="Stretch">
        <staticText>
            <reportElement uuid="2322e160-4c43-4027-af14-3c69a4d236ed" x="8" y="4" width="100" height="20" backcolor="#003333"/>
            <textElement/>
            <text><![CDATA[First_name]]></text>
        </staticText>
        <staticText>
            <reportElement uuid="a5f84e13-ffee-491f-aa52-602b7b4e9c99" x="149" y="4" width="100" height="20" forecolor="#9999FF" backcolor="#00FFFF"/>
            <textElement/>
            <text><![CDATA[Last_name]]></text>
        </staticText>
        <staticText>
            <reportElement uuid="cd216988-6061-4ef2-8f4d-18e31cabd9e1" x="299" y="4" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[Designation]]></text>
        </staticText>
        <staticText>
            <reportElement uuid="22587697-d3b8-4a55-b81d-d380a4e19a7a" x="433" y="4" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[Department]]></text>
        </staticText>
    </band>
</columnHeader>
<detail>
    <band height="26" splitType="Stretch">
        <textField>
            <reportElement uuid="3f6821a6-3aa1-418f-aa68-25b844256276" key="First_name" x="8" y="0" width="100" height="21"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{First_name}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement uuid="e453bc45-a734-4db6-bba5-19a24252d3ba" x="149" y="0" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Last_name}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement uuid="5c604a9d-5dba-4b47-9e8c-41de0a29323c" x="299" y="0" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Designation}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement uuid="fa2e53f2-ff71-485d-b9ef-0259ace3eabb" x="433" y="0" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Department}]]></textFieldExpression>
        </textField>
    </band>
</detail>
<columnFooter>
    <band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
    <band height="42" splitType="Stretch"/>
</summary>
</jasperReport>

While i run GetResult.java it gives following error:

net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : First_namenet.sf.jasperreports.engine.JRException: Error retrieving field value from bean : First_name
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:1577)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:149)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:932)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:864)
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.fillReport(JasperFillManager.java:969)
at GetResult.result(GetResult.java:78)
at GetResult.main(GetResult.java:99)
Caused by: java.lang.NoSuchMethodException: Unknown property 'First_name'
at org.apache.commons.beanutils.PropertyUtils.getSimpleProperty(PropertyUtils.java:1175)
at org.apache.commons.beanutils.PropertyUtils.getNestedProperty(PropertyUtils.java:772)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:801)
at     net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
... 14 more

Please help me to resolve this problem.


Answer:

Rename your fields to start with lowercase letters, and your getter and setter methods to correspond. Like:

String firstName;
public String getFirstName() {
    return firstName;
} 
public void setFirstName(String First_name) {
        this.firstName= First_name;
}

Jasper looks for the corresponding getter that respects java convensions.

Question:

I have a .jrxml file and I would like to pass some params from the code to it. I have an Order class that has fields like double price, int quantity and Product product. The situation is simple, when i need to pass price or quantity, I just do something like this:

<textFieldExpression class = "java.lang.Integer">
   <![CDATA[$F{quantity}]]>
</textFieldExpression>

The problem appears when I try to pass product.getName(). I tried something like:

<textFieldExpression class = "java.lang.String">
   <![CDATA[$F{product}.getName()]]>
</textFieldExpression>

and many others but I keep getting error: net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 1. Field not found : product

Do you have any idea how to solve this problem?


Answer:

For example you have a pair of JavaBeans (POJO):

public class Order {

    private double price;
    private int quantity;
    private Product product;
    // public getters 
}

public class Product {

    private String name;
    // public getters 
}

and you declare report's datasource in the manner like this: (yes, I like Guava)

JRBeanCollectionDataSource datasource = new JRBeanCollectionDataSource(Lists.newArrayList(ImmutableList.<Order>builder()
        .add(new Order(1000.2, 10, new Product("Phone")))
        .add(new Order(10200.0, 2, new Product("Tv")))
        .build()));

In case using this fields declaration:

<field name="order" class="java.lang.Object">
    <fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
<field name="price" class="java.lang.Double"/>
<field name="quantity" class="java.lang.Integer"/>
<field name="productName" class="java.lang.String">
    <fieldDescription><![CDATA[product.name]]></fieldDescription>
</field>

you can use such expressions:

<textField>
    <reportElement x="0" y="0" width="100" height="30"/>
    <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
</textField>
<textField>
    <reportElement x="100" y="0" width="100" height="30"/>
    <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
</textField>
<textField>
    <reportElement x="200" y="0" width="100" height="30"/>
    <textFieldExpression><![CDATA[$F{productName}]]></textFieldExpression>
</textField>

Note:

  • Don't forget that getters should be public
  • More info: JavaBean Data Sources
  • The good explanation of _THIS using with samples can be found in this posts:
    1. How to access the root element of the datasource in jasperreports
    2. Passing the List of primitive type objects as datasource for subreport
    3. How do I print a list of strings contained within another list in iReport?

Question:

I have Java bean with the field activeRecord

private Boolean activeRecord;

@Override
public Boolean isActiveRecord() {
    return activeRecord;
}

@Override
public void setActiveRecord(Boolean activeRecord) {
    this.activeRecord = activeRecord;
}

when I send it in List as Jasper Report data source

List<Branch> dataList = new BranchLogic().selectAll();
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);

I get the error message

net.sf.jasperreports.engine.JRException: Error retrieving field value from bean: activeRecord. .... Caused by: java.lang.NoSuchMethodException: Property 'activeRecord' has no getter method in class 'class com.tawaak.app.data.domain.model.branch.Branch'

Why Jasper doesn't recognize the isActiveRecord as a getter method?


Answer:

The prefix is... can be used for methods that return a primitive boolean. However, your field activeRecord is of type Boolean, which is an object (the wrapper type of boolean), and for objects you always need to use get....

From the JavaBeans specification, 8.3.2:

In addition, for boolean properties, we allow a getter method to match the pattern:

public boolean is<PropertyName>();

This is<PropertyName> method may be provided instead of a get<PropertyName> method, or it may be provided in addition to a get<PropertyName> method.

As such, you have two possible fix:

  • Make your activeRecord a boolean and keep the getter isActiveRecord(). This would be the preferred approach if activeRecord cannot be null.
  • Keep it as a Boolean, but rename your method isActiveRecord() to getActiveRecord(). You'll need to make sure the caller handles null properly.

Question:

I have this object structure, to use like Java Bean in mi jasper report,

public class Person{
   private String name;
   private String lastname;
   private List<Job> jobs;
}

public class Job{
   private String jobName;
   private String companyName;
   private List<Reponsability> responsabilities;
}

All class with corresponding setters and getters

When I create a list into jasper report I define the "job list" JRDataSource expression this way:

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{jobs})

Now I want to create another list, "responsibilities list" on the "job list", the question is:

How can I define the data source to get the corresponding job to pass into the JRDataSource Expression?

I imagine something like this:

new JRBeanCollectionDataSource(${jobs}.get($index).getResponsabilities())

but I can't get it work this


Answer:

I found the solution to do this:

Into the list, I created a variable called jobInstance

    <field name="jobInstance" class="com.mypackage.example.Job">
        <fieldDescription><![CDATA[_THIS]]></fieldDescription>
    </field>

Then when I set the datasource for responsibilities list did this:

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{jobInstance}.getResponsabilities())

References: https://community.jaspersoft.com/questions/508346/getting-current-object

Question:

I've added DS to my report but I have an error

Why in error does not have a field on which the error occurs I can not prescribe anything in the .jrxml

17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1) net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : 
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:1330)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1231)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1207)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1554)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:149)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:841)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:88)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:653)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:569)
17:01:38,172 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:915)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at by.softclub.abank.admin.ui.reports.ReportBuilder.dataOutFromBeans(ReportBuilder.java:163)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at by.softclub.abank.admin.ui.reports.ReportBuilder.addByteDataToReport(ReportBuilder.java:65)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at by.softclub.abank.admin.ui.page.AbstractBasePage.getbuildReport(AbstractBasePage.java:197)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at by.softclub.abank.admin.ui.page.AbstractBasePage.printReport(AbstractBasePage.java:170)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at by.softclub.abank.admin.ui.page.UserInfoStatisticPage.printActiveUsersReport(UserInfoStatisticPage.java:152)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at java.lang.reflect.Method.invoke(Method.java:597)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.el.parser.AstValue.invoke(AstValue.java:258)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
17:01:38,173 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.primefaces.application.DialogActionListener.processAction(DialogActionListener.java:45)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at javax.faces.component.UICommand.broadcast(UICommand.java:315)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:98)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at by.softclub.abank.admin.ui.faces.UrlFilter.doFilter(UrlFilter.java:118)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145)
17:01:38,175 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97)
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102)
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336)
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653)
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920)
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1)  at java.lang.Thread.run(Thread.java:662)
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1) Caused by: java.lang.NoSuchMethodException: Unknown property '' on class 'class by.softclub.common.entity.UserBPSWrapper'
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322)
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1)  at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1)  at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
17:01:38,176 ERROR [stderr] (http-/0.0.0.0:7051-1)  ... 54 more

So my bean

public class UserBPSWrapper implements Serializable, IWrapper {

    private String id;
    private String numberSubdivision;
    private String fioAdmin;
    private Date dateReg;
    private String fioClient;
    private String numberPhone;
    private String numberPassport;
    private Date dateRegClient;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNumberSubdivision() {
        return numberSubdivision;
    }

    public void setNumberSubdivision(String numberSubdivision) {
        this.numberSubdivision = numberSubdivision;
    }

    public String getFioAdmin() {
        return fioAdmin;
    }

    public void setFioAdmin(String fioAdmin) {
        this.fioAdmin = fioAdmin;
    }

    public Date getDateReg() {
        return dateReg;
    }

    public void setDateReg(Date dateReg) {
        this.dateReg = dateReg;
    }

    public String getFioClient() {
        return fioClient;
    }

    public void setFioClient(String fioClient) {
        this.fioClient = fioClient;
    }

    public String getNumberPhone() {
        return numberPhone;
    }

    public void setNumberPhone(String numberPhone) {
        this.numberPhone = numberPhone;
    }

    public String getNumberPassport() {
        return numberPassport;
    }

    public void setNumberPassport(String numberPassport) {
        this.numberPassport = numberPassport;
    }

    public Date getDateRegClient() {
        return dateRegClient;
    }

    public void setDateRegClient(Date dateRegClient) {
        this.dateRegClient = dateRegClient;
    }
}

I call report

JRDataSource dataSource = createReportDataSource(reportRows);
String jasperReportName = newPath + templateXML + ".jasper";

jasperPrint = JasperFillManager.fillReport(jasperReportName, reportParametersMap, dataSource);

private JRDataSource createReportDataSource(Collection reportRows) {
    JRDataSource dataSource;

    if (reportRows == null || reportRows.size() == 0) {
        dataSource = new JREmptyDataSource();
    } else {
        dataSource = new JRBeanCollectionDataSource(reportRows);
    }
    return dataSource;
}
setListToReportDetails((List<UserBPSWrapper>) dataBPSModel.getWrappedData());

My JRXML

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Subdevelopers" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="bfa8e4d4-05e1-4c4c-85de-9feb417ff0eb">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <parameter name="subdivisionName" class="java.lang.String"/>
    <parameter name="dateFrom" class="java.lang.String"/>
    <parameter name="dateTo" class="java.lang.String"/>
    <parameter name="reportDate" class="java.lang.String"/>
    <parameter name="clientsCountText" class="java.lang.String"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="numberSubdivision" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="fioAdmin" class="java.lang.String"/>
    <field name="dateReg" class="java.util.Date"/>
    <field name="fioClient" class="java.lang.String"/>
    <field name="numberPhone" class="java.lang.String"/>
    <field name="numberPassport" class="java.lang.String"/>
    <field name="dateRegClient" class="java.util.Date">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <title>
        <band height="85" splitType="Stretch">
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="0" y="0" width="380" height="14" uuid="cded2592-f09e-4c61-89d6-f5e5d143035e"/>
                <textElement markup="none">
                    <font fontName="SansSerif" size="10" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{subdivisionName}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="0" y="14" width="90" height="14" uuid="c90da0c8-1336-4dc3-a021-f0e3ae942b2d">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement>
                    <font fontName="SansSerif" size="10"/>
                </textElement>
                <text><![CDATA[Период отчета с:]]></text>
            </staticText>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="90" y="14" width="70" height="14" uuid="53044afe-bd20-42a6-97fb-e6b8b5e81b83"/>
                <textElement markup="none">
                    <font fontName="SansSerif" size="10" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{dateFrom}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="160" y="14" width="20" height="14" uuid="10a6a7db-dd2a-4bb6-8f06-25a03e8bd5d9">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement>
                    <font fontName="SansSerif" size="10"/>
                </textElement>
                <text><![CDATA[РїРѕ]]></text>
            </staticText>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="180" y="14" width="70" height="14" uuid="878bcb56-dc60-4bb5-9909-73506e8be23e"/>
                <textElement markup="none">
                    <font fontName="SansSerif" size="10" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{dateTo}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="0" y="28" width="380" height="14" uuid="6a7930c5-5605-493f-8dc4-cf3d60f9131a">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement>
                    <font fontName="SansSerif" size="10"/>
                </textElement>
                <text><![CDATA[Банк: ОАО "БПС-Сбребанк"]]></text>
            </staticText>
            <staticText>
                <reportElement x="0" y="42" width="180" height="14" uuid="8e29e107-6761-48f6-9ce9-3a430f2ad173">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <text><![CDATA[Р"ата Рё время формирования отчета:]]></text>
            </staticText>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="180" y="42" width="200" height="14" uuid="c0532bd1-edbc-4d78-b126-239cdfd3b818"/>
                <textElement markup="none">
                    <font fontName="SansSerif" size="10" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{reportDate}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <columnHeader>
        <band height="31" splitType="Stretch">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <rectangle>
                <reportElement x="0" y="0" width="20" height="31" uuid="fab33adb-f537-46e1-ae19-aed14e0d553a"/>
            </rectangle>
            <staticText>
                <reportElement x="0" y="0" width="20" height="30" uuid="8b0c0311-2893-404d-a5f6-759c71585b39">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="8"/>
                </textElement>
                <text><![CDATA[в„–]]></text>
            </staticText>
            <rectangle>
                <reportElement x="20" y="0" width="100" height="31" uuid="4c6d0de7-16be-4584-a292-b323d1765921"/>
            </rectangle>
            <staticText>
                <reportElement x="20" y="0" width="100" height="30" uuid="90ea0ab1-7105-4ae0-8a27-74db2a3d5a88">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="8"/>
                </textElement>
                <text><![CDATA[Р¤РРћ администратора]]></text>
            </staticText>
            <rectangle>
                <reportElement x="120" y="0" width="70" height="31" uuid="b7cdcd71-072e-4406-bebd-8b8f29b145f3"/>
            </rectangle>
            <staticText>
                <reportElement x="120" y="0" width="70" height="30" uuid="17cc09fb-826a-4fc3-bf1f-8f56e54cfbd3">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="8"/>
                </textElement>
                <text><![CDATA[Р"ата регистрации]]></text>
            </staticText>
            <rectangle>
                <reportElement x="190" y="0" width="100" height="31" uuid="76f7b8c5-8418-4cb6-a7a1-17afbce4cfbf">
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
            </rectangle>
            <staticText>
                <reportElement x="190" y="0" width="100" height="30" uuid="9366a903-3ba0-492b-b2b9-1a5af4080975">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="8"/>
                </textElement>
                <text><![CDATA[Р¤РРћ клиента]]></text>
            </staticText>
            <rectangle>
                <reportElement x="290" y="0" width="100" height="31" uuid="09f7a047-ab01-4e01-a268-5dcbcd2fae49">
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
            </rectangle>
            <staticText>
                <reportElement x="290" y="0" width="100" height="30" uuid="f31660cc-44c3-4646-a5e5-71a7c0935520">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="8"/>
                </textElement>
                <text><![CDATA[Номер мобильного]]></text>
            </staticText>
            <rectangle>
                <reportElement x="390" y="0" width="100" height="31" uuid="ac78bb73-ac1c-49b2-ba4f-292f2453dec4">
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
            </rectangle>
            <staticText>
                <reportElement x="390" y="0" width="100" height="30" uuid="f76e2870-bd7a-4099-9a53-5916974b7fe2">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="8"/>
                </textElement>
                <text><![CDATA[Л/Н паспорта]]></text>
            </staticText>
            <rectangle>
                <reportElement x="490" y="0" width="70" height="31" uuid="c932d60f-efe1-421d-8fbe-a66f6d09eb10"/>
            </rectangle>
            <staticText>
                <reportElement x="490" y="0" width="70" height="30" uuid="de94e44b-9faa-4835-81be-651662d3a0a3">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="8"/>
                </textElement>
                <text><![CDATA[Р"ата регистрации клиентом]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="31" splitType="Stretch">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <rectangle>
                <reportElement x="0" y="0" width="20" height="31" uuid="944528be-a403-4ed1-8ece-6c6d40cece23"/>
            </rectangle>
            <rectangle>
                <reportElement x="20" y="0" width="100" height="31" uuid="d65b1141-6bf4-431e-996a-a56ee713e7aa"/>
            </rectangle>
            <rectangle>
                <reportElement x="120" y="0" width="70" height="31" uuid="8440dbcb-221e-452a-9188-322c0fc00bdd"/>
            </rectangle>
            <rectangle>
                <reportElement x="190" y="0" width="100" height="31" uuid="03976249-b504-4218-b151-06aa198fe1bd">
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
            </rectangle>
            <rectangle>
                <reportElement x="290" y="0" width="100" height="31" uuid="28dbda54-1653-4559-b5ea-fcee2fa497b2">
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
            </rectangle>
            <rectangle>
                <reportElement x="390" y="0" width="100" height="31" uuid="be18db82-9a46-4e54-b2d1-98f2bc66af77">
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
            </rectangle>
            <rectangle>
                <reportElement x="490" y="0" width="70" height="31" uuid="0c336ccd-ea62-4e10-b521-69b85599592d"/>
            </rectangle>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="0" y="0" width="20" height="30" uuid="d0d3ca75-e0ed-4605-9b35-9e25a9d61b89"/>
                <textElement verticalAlignment="Middle" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{numberSubdivision}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="20" y="0" width="100" height="30" uuid="15faac99-3883-445e-b399-80aed7b8639e"/>
                <textElement verticalAlignment="Middle" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{fioAdmin}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="120" y="0" width="70" height="30" uuid="350cccbb-0c58-4136-8306-278195115415"/>
                <textElement verticalAlignment="Middle" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{dateReg}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="190" y="0" width="100" height="30" uuid="d35bafcc-3666-4fdb-a3e3-dfd5242bc7df"/>
                <textElement verticalAlignment="Middle" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{fioClient}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="290" y="0" width="100" height="30" uuid="29f916c0-00a5-4d08-8c69-5673dbb36e59"/>
                <textElement verticalAlignment="Middle" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{numberPhone}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="390" y="0" width="100" height="30" uuid="f24050a4-70c1-4fdb-ba88-03c0ea49445c"/>
                <textElement verticalAlignment="Middle" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{numberPassport}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="490" y="0" width="70" height="30" uuid="e47a9534-4964-4208-8e0d-d6e03c65754e"/>
                <textElement verticalAlignment="Middle" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{dateRegClient}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <pageFooter>
        <band height="70" splitType="Stretch">
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="0" y="0" width="560" height="24" uuid="4608c8a8-f63c-4b04-8c48-9bdb0a8f085a">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement markup="none">
                    <font fontName="SansSerif" size="10" isBold="false"/>
                    <paragraph leftIndent="240"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{clientsCountText}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="0" y="0" width="240" height="14" uuid="05f5423b-164a-4754-acca-5c064adef821">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <text><![CDATA[Количество зарегистрированных клиентов, всего:]]></text>
            </staticText>
            <staticText>
                <reportElement x="0" y="30" width="80" height="14" uuid="d90bb3fc-d0ff-44a9-ba75-bdcccd3ddf87">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <text><![CDATA[Отчет проверил ]]></text>
            </staticText>
            <line>
                <reportElement x="80" y="44" width="100" height="1" uuid="3d0fceff-a0dd-4b79-93aa-aa5e9158ef27"/>
                <graphicElement>
                    <pen lineWidth="1.0" lineStyle="Solid"/>
                </graphicElement>
            </line>
            <staticText>
                <reportElement x="180" y="30" width="10" height="14" uuid="fe722733-55c0-4cae-b24d-5669fc15c2ba">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <text><![CDATA[(]]></text>
            </staticText>
            <staticText>
                <reportElement x="290" y="30" width="10" height="14" uuid="3d078cf1-0d6b-4083-8652-204e1ad15f99">
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <text><![CDATA[)]]></text>
            </staticText>
            <line>
                <reportElement x="190" y="44" width="100" height="1" uuid="4a4d46de-8544-4f79-bc2b-0356e9468edf"/>
                <graphicElement>
                    <pen lineWidth="1.0" lineStyle="Solid"/>
                </graphicElement>
            </line>
        </band>
    </pageFooter>
</jasperReport>

Answer:

The dateRegClient field in the report has an empty fieldDescription. Field descriptions are used to map field to bean property names, whence the error.

Removing the field description or setting it to dateRegClient should get rid of the error.

Question:


Answer:

You have two options (I'm not considering that you can create a method on the CustomerJ bean):

Define a field and in description indicate path to property.

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

OR

Define a field as _THIS that represents the base class CustomerJ

<field name="_THIS" class="com.your.package.CustomerJ"/>

In expression call methods on that class, as example in textField

<textField>
    <reportElement x="0" y="0" width="100" height="20" uuid="5884d3b9-3239-4d2e-af3e-ee5163da3d3b"/>
    <textFieldExpression><![CDATA[$F{_THIS}.getBillingAddress().getAddress1()]]></textFieldExpression>
</textField>

Question:

I am developing a report on Jaspersoft Studio 6.4.0 and I use JavaBeans as data sources.

I need to change the background color of a textfield dynamically. In fact I have a color variable in my bean which is calculated like this :

Color resultColor = new Color(255, (int) (ratio * 5.1), 0);

Where ratio is a double calculated with the variable in the textfield.

I have two possibilities, the first one is to give a Color to my jasper report and the second one is to calculate the green and red ratio and make the color in a conditionnal style. I don't know where I can put this kind of dynamic color, can you help me?


Answer:

I just find the solution:

<propertyExpression name="net.sf.jasperreports.style.backcolor"> 
    <![CDATA["#" + net.sf.jasperreports.engine.util.JRColorUtil.getColorHexa($F‌​{resultColor})]]> 
</propertyExpression> 

With resultColor a java.awt.color. You need to put this in your report element.

I find the solution here

Question:

I want to map my DataBean(TestModelA) to existing XML, but JasperReports gives me an exception. The problem is that I have custom field (TestModelB) and I have public getters and setters in that class, but jasper does not recognize them. How can i solve this problem?

TestModelA:

public class TestModelA {
private Long id;
private String label_key;
private TestModelB testModelB;
public TestModelA(Long id, String label_key, TestModelB testModelB) {
    this.id = id;
    this.label_key = label_key;
    this.testModelB = testModelB;
}
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getLabel_key() {
    return label_key;
}
public void setLabel_key(String label_key) {
    this.label_key = label_key;
}
public TestModelB getTestModelB() {
    return testModelB;
}
public void setTestModelB(TestModelB testModelB) {
    this.testModelB = testModelB;
}
}

TestModelB:

public class TestModelB {

private Long owner;
public TestModelB(Long owner) { this.owner = owner;   }

public Long getOwner() {
    return owner;
}

public void setOwner(Long owner) {
    this.owner = owner;
}
}

My XML file:

 <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report3" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="077cd490-b99c-4ed1-ad76-b15714625957">
        <property name="ireport.zoom" value="2.1435888100000016"/>
        <property name="ireport.x" value="94"/>
        <property name="ireport.y" value="0"/>
        <queryString>
            <![CDATA[SELECT MODULE.ID, MODULE.LABEL_KEY, MODULE.OWNER FROM MODULE]]>
        </queryString>
        <field name="id" class="java.lang.Long"/>
        <field name="label_key" class="java.lang.String"/>
        <field name="owner" class="java.lang.Long"/>
        <background>
            <band splitType="Stretch"/>
        </background>
        <columnHeader>
            <band height="21" splitType="Stretch">
                <staticText>
                    <reportElement x="0" y="0" width="185" height="20" uuid="68ea9590-9d19-4bc5-9540-d059208b0ce8"/>
                    <text><![CDATA[id]]></text>
                </staticText>
                <staticText>
                    <reportElement x="185" y="0" width="185" height="20" uuid="325690ac-7218-4000-8ec6-a492d185477a"/>
                    <text><![CDATA[label_key]]></text>
                </staticText>
                <staticText>
                    <reportElement x="370" y="0" width="185" height="20" uuid="20521e4b-9057-42a9-8adc-10309abc8935"/>
                    <text><![CDATA[owner]]></text>
                </staticText>
            </band>
        </columnHeader>
        <detail>
            <band height="23" splitType="Stretch">
                <textField>
                    <reportElement x="0" y="0" width="185" height="20" uuid="11901f0e-b03d-47f8-a8aa-35bab36d26bf"/>
                    <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="185" y="0" width="185" height="20" uuid="b715cb6b-3650-4266-a1cf-552ee35bc08f"/>
                    <textFieldExpression><![CDATA[$F{label_key}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="370" y="0" width="185" height="20" uuid="5c41b534-a74e-4b91-b35d-fcbb88789000"/>
                    <textFieldExpression><![CDATA[$F{owner}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
    </jasperReport>

I am getting exception:

 Exception in thread "main" net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : owner
        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:1330)
        at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1231)
        at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1207)
        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.fillReport(JasperFillManager.java:969)
        at net.didorenko.Reporter.creatReport(Reporter.java:99)
        at net.didorenko.Reporter.threeReport(Reporter.java:78)
        at net.didorenko.Reporter.main(Reporter.java:28)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
    Caused by: java.lang.NoSuchMethodException: Unknown property 'owner' on class 'class net.didorenko.model.TestModelA'
        at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1313)
        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)
        ... 20 more

Answer:

Your are passing TestModelA class as datasource you need either to create the method in this class or reference the method correctly from jasper report.

Solution 1 Create method in TestModelA

public Long getOwner() {
    return getTestModelB().getOwner(); //Handle NullPointer?
}

Solution 2 Reference correctly in jrxml

<field name="testModelB" class="net.didorenko.model.TestModelB"/>
...
<textField>
    <reportElement x="370" y="0" width="185" height="20" uuid="5c41b534-a74e-4b91-b35d-fcbb88789000"/>
    <textFieldExpression><![CDATA[$F{testModelB}.getOwner()]]></textFieldExpression>
</textField>

Question:

I have a spring bean:

<bean 
    id="my.package.CustomerWrapper" 
    class="my.package.CustomerWrapper">

    <property name="customerWrapperCache" ref="customerWrapperCache"></property>
</bean>

And in a jrxml:

<variable name="a_Info" class="java.lang.String" resetType="None">
    <variableExpression><![CDATA[my.package.CustomerWrapper.lookupWithMainAddress($P{cust_id}, "informationLine")]]></variableExpression>
</variable>

But this is throwing a NullPointerException because customerWrapperCache is null.

What is the correct way to do this?


Answer:

JasperReports is not using the CustomerWrapper bean that is managed by Spring, it is creating a new instance, which is why the customerWrapperCache is null;

To use a Spring bean inside a Jasper report, you need to pass the bean as a parameter to the report. See the Jasper Reports - Populating Model and View on the Spring Reference site.

Question:

Object is like this

class jobVO{ 
    ...
    List<taskVO> tasks;
    ...
}
class TaskVO{
    String name;
    ...
}  

I need to pass List to a jrxml and print.

I tried with subreports. but there I couldn't find a way to pass the params. Also tried with collection datasource also. There the problem I found was need to mention the factory method. My requirement is to create this report dynamically.


Answer:

created 2 subreports

<detail>
    <band height="60" splitType="Stretch">
        <subreport>
            <reportElement x="-19" y="34" width="560" height="26" uuid="7006626b-cfcb-4dcc-a1c8-3ef0f95e0440"/>
            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{items})]]></dataSourceExpression>
            <subreportExpression><![CDATA["D:\\Root1\\WEB1\\src\\sub1.jasper"]]></subreportExpression>
        </subreport>
    </band>
</detail>

Question:

Struggling with this for a week, and yes I progressed slightly thanks to previous posts on the same subject however, not completely.

Two reports:

Master Report - Contains company info
Sub Report - Contains 1..* company orders in detail list

The data source is built from pojo array lists:

Master Report:

<parameter name="SUB_REPORT" class="java.lang.Object"/>
<queryString>
  <![CDATA[]]>
</queryString>
<field name="companyName" class="java.lang.String">
  <fieldDescription><![CDATA[companyName]]></fieldDescription>
</field>
<field name="companyOrders" class="java.util.List">
  <fieldDescription><![CDATA[companyOrders]]></fieldDescription>
</field>
<background>
  <band splitType="Stretch"/>
</background>
<pageHeader>
  <band height="214" splitType="Stretch">
    <textField>
      <reportElement x="415" y="5" width="40" height="14" uuid="f03490b8-4ab1-4d61-a0d6-f43214dd1c1c"/>
      <textElement>
        <font isBold="false"/>
        <paragraph lineSpacing="Single" lineSpacingSize="2.0"/>
      </textElement>
      <textFieldExpression><![CDATA["Date:"]]></textFieldExpression>
    </textField>
    <textField>
      <reportElement x="1" y="110" width="99" height="14" uuid="e82847e9-7235-45d6-9eeb-0f5d2fc2622c"/>
      <textElement>
        <font isBold="false"/>
        <paragraph lineSpacing="Single" lineSpacingSize="2.0"/>
      </textElement>
      <textFieldExpression><![CDATA["Business Name:"]]></textFieldExpression>
    </textField>    
    <textField>
      <reportElement x="113" y="110" width="252" height="15" uuid="68e891b9-cd70-4b65-90f3-b11866446f4a"/>
      <textFieldExpression><![CDATA[$F{companyName}]]></textFieldExpression>
    </textField>
  </band>
</pageHeader>
<columnHeader>
  <band height="17" splitType="Stretch">
    <staticText>
      <reportElement x="0" y="2" width="100" height="15" uuid="309582cb-bde5-4a37-a97b-c9058fc5e184"/>
      <text><![CDATA[Order Num]]></text>
    </staticText>
    <staticText>
      <reportElement x="110" y="2" width="100" height="15" uuid="a6fb5059-8126-4ae2-be9c-a5d0ed700ef9"/>
      <text><![CDATA[Order Amount]]></text>
    </staticText>
    <staticText>
      <reportElement x="220" y="2" width="100" height="15" uuid="198d15de-1484-41cc-b2b0-66b8a75c106d"/>
      <text><![CDATA[Order Date]]></text>
    </staticText>
    <staticText>
      <reportElement x="369" y="2" width="100" height="15" uuid="d142e6aa-0ff5-416f-b367-d639a287aadd"/>
      <text><![CDATA[Order Paid]]></text>
    </staticText>
  </band>
</columnHeader>
<detail>
  <band height="28" splitType="Stretch">
    <subreport>
      <reportElement x="1" y="4" width="468" height="21" uuid="77507436-37c6-4f8e-bc13-3997be8ffbee"/>
      <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{companyOrders})]]></dataSourceExpression>
      <subreportExpression><![CDATA[$P{SUB_REPORT}]]></subreportExpression>
    </subreport>
  </band>
</detail>

Sub Report:

<field name="orderNum" class="java.lang.Long">
    <fieldDescription><![CDATA[orderNum]]></fieldDescription>
</field>
<field name="orderTotal" class="java.lang.Double">
    <fieldDescription><![CDATA[orderTotal]]></fieldDescription>
</field>
<field name="orderDate" class="java.sql.Timestamp">
    <fieldDescription><![CDATA[orderDate]]></fieldDescription>
</field>
<field name="orderPaid" class="java.lang.Integer">
    <fieldDescription><![CDATA[orderPaid]]></fieldDescription>
</field>
<background>
    <band splitType="Stretch"/>
</background>
<detail>
    <band height="24" splitType="Stretch">
        <textField>
            <reportElement x="0" y="3" width="100" height="15" uuid="d356cc51-325b-43c3-ab2d-dc9f5041d3e4"/>
            <textFieldExpression><![CDATA[$F{orderNum}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="110" y="3" width="100" height="15" uuid="d91fd44f-12f9-4c67-aaca-24c7efa6c75c"/>
            <textFieldExpression><![CDATA[$F{orderTotal}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="222" y="3" width="100" height="15" uuid="af910428-2309-4159-ba5e-c29748f00b6f"/>
            <textFieldExpression><![CDATA[$F{orderDate}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="369" y="3" width="100" height="15" uuid="2921437b-8146-41e4-86fa-6b5ec39f4d65"/>
            <textFieldExpression><![CDATA[$F{orderPaid}]]></textFieldExpression>
        </textField>
    </band>
</detail>

The java code that I use to compile and print the reports at runtime:

CompanyMapper cm = new CompanyMapper();         
this.company = cm.getCompanyByID((Long) httpRequest.getSession().getAttribute("userCompanyID"));
List<ACompany> c = new ArrayList<ACompany>();
List<AOrder> companyOrders = getOrders(getParameterInt("orderStatus"));

c.add(this.company);

String reportFolder = httpRequest.getServletContext().getRealPath("/").replace('\\', '/') + "reports/";         
String invoice = reportFolder + ReportManager.getProperty("orders");            
String orders = reportFolder + ReportManager.getProperty("orderList");
File invoiceFile = new File(invoice);
File orderFile = new File(orders);
msgHelper = new MessageHelper();        

JasperReport jasperMasterReport = JasperCompileManager.compileReport(invoiceFile.getAbsolutePath());
JasperReport jasperSubReport = JasperCompileManager.compileReport(orderFile.getAbsolutePath());

Map<String,Object> parameters = new HashMap<String,Object>();                       
parameters.put("SUB_REPORT", jasperSubReport);
parameters.put("SUB_REPORT_DATA", new JRBeanCollectionDataSource(companyOrders));

JasperFillManager.fillReport(jasperSubReport, parameters, new JRBeanCollectionDataSource(companyOrders));
JasperPrint print = JasperFillManager.fillReport(jasperMasterReport, parameters, new JRBeanCollectionDataSource(c));            

Company info show up fine in the Master report however, no orders in details are printed from the sub report. When I print the subreport, there is data (ie, detail of orders) showing up as expected. I just want to pass the subreport in the detail section of the master, and everything will be perfect.


Answer:

If you want to use "companyOrders" as a field in your reports you have to put it in your "ACompany" POJO, more or less like that :

public class DataBean {
    private String companyName;
    private List<AOrder> companyOrders;
}