jasper-reports

Filling a Jasper Report but getting nulls


Unless I read the documentation wrong, I need to use JasperFillManager.fillReport to populate a jasper report. The field variables are noted as: $F{myVarName}

For instance. I have for this example 3 <textField> nodes in my jrxml each text field contains a <textFieldExpression> node like this:

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

I've built a service that can generate pdf as base64 that I can inject where I need it. Here is an excerpt

PdfTemplate template = templateRepo.findByTemplateId(templateId)
JasperReport jasperReport = JasperCompileManager.compileReport(new ByteArrayInputStream(template.getTemplateXml().getBytes()))
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters)
//JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource) //Also tried this. 

Parameters looks as follows:

Map<String, Object> parameters = new HashMap<>();

parameters.put("param1", "Value of param1");
parameters.put("param2", "Value of param2");
parameters.put("param3", "Value of param3");
//Where param1-3 correspond with $F{param1-3} 

When the report is generated, I only see nulls.

ie:

Parameter 1: null

Parameter 2: null

Parameter 3: null

Any help to get those parameter values to pull to the report will be appreciated.

Reference Notes:

https://javadoc.io/static/net.sf.jasperreports/jasperreports/4.0.0/net/sf/jasperreports/engine/JasperFillManager.html

https://www.baeldung.com/spring-jasper


Solution

  • In Jasper Reports, $F{} is for a field, not for a parameter. Change your expression to $P{}

    Also, make sure the parameter is defined in the JRXML.

    So you textFieldExpression should be as below.

    <textFieldExpression class="java.lang.String"><![CDATA[$P{param1}]]></textFieldExpression>