Search code examples
javajasper-reports

Jasper Report Print Custom Objects in Arraylist


I am passing a List as paramter to Jasper as following.

Map<String, Object> model=new HashMap<>();
  List<CustomObject> issues=new ArrayList<>();
            issues.add(new CustomObject(1,"AIRPORT Services","XYZ","asdfa","asdf","asddf"));
            issues.add(new CustomObject(1,"AIRPORT Services","XYZ","asdfa","asdf","asddf"));
            model.put("issues",issues);
            JasperPrint jasperPrint1 = JasperFillManager.fillReport(report, model, new JREmptyDataSource());

Now I am able to retrieve issuesList in jasper but I can't retrieve value inside CustomObject.

Following works and prints reference of CustomObject iterated using following

<textFieldExpression><![CDATA[$P{list}.get($V{ROW_INDEX})]]></textFieldExpression>

This throws exception when I want to access value of field inside Custom Object such as

<textFieldExpression><![CDATA[$P{list}.get($V{ROW_INDEX}).getCustomMethod()]]>

Exception:

Exception obtained is: The method getCustomMethod() is undefined for the type Object value = ((java.util.List)parameter_list.getValue()).get(((java.lang.Integer)variable_ROW_INDEX.getValue())).getCustomMethod(); //$JR_EXPR_ID=0$

With Help of Mike Answer at Print an arraylist content with JasperReports I have iterated my Arraylist in jasper. Any help highly appreciated.


Solution

  • This worked for me when I just cast from Object to CustomObject like as follow

    <textFieldExpression><![CDATA[((com.custom.CustomObject)$P{flightIssues}.get($V{ROW_INDEX})).getCustomeMethod()]]></textFieldExpression>