Connect master report and subreport - passing list

2019-05-10 05:22发布

I'm using the iReport 4.5.1 and I've faced a problem with connecting master report and subreport.

I have two objects:

  • Account (accountID, date, listOfParagraphs) - all fields are defined in master report;
  • Paragraph (account, paragraphNo, someObject) - all fields defined in subreport(stavke.jrxml).

The listOfParagraphs is defined as java.util.List<Paragraph> (List of objects) and I want to pass it to subreport.

I've defined this subreport's properties in master report:

  • Subreport expression: stavke.jasper
  • Expression class: java.lang.String
  • Connection type: Use a datasource expression
  • Datasource expression:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{listOfParagraphs})

I get this error:

net.sf.jasperreports.engine.JRException: Error loading object from file : stavke.jasper
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:101)
at net.sf.jasperreports.engine.util.JRLoader.loadObjectFromLocation(JRLoader.java:253)
at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateReport(JRFillSubreport.java:301)
    .....

I've tried so hard about this, but i can't solve it.

Can someone help me, please?

@Vyccus (edited)

I've changed paths a bit:

<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["C:\\Documents and Settings\\D\\My Documents\\NetBeansProjects\\Project\\subreport\\"]]></defaultValueExpression>
</parameter>

and

<subreport>
            <reportElement x="0" y="42" width="555" height="100"/>
            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{ls})]]></dataSourceExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "stavke.jasper"]]></subreportExpression>
        </subreport>

Now i get this:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.lang.Class.isAssignableFrom(Native Method)
at net.sf.jasperreports.engine.fill.JRFillTextField.getFormat(JRFillTextField.java:706)
at net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java:394)
at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:368)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:258)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:499)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2036)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:760)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:270)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:128)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:946)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:864)
at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:613)
at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59)
at net.sf.jasperreports.engine.fill.JRThreadSubreportRunner.run(JRThreadSubreportRunner.java:205)
at java.lang.Thread.run(Thread.java:662)

1条回答
Anthone
2楼-- · 2019-05-10 05:51

Java Code:

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

//if you want this to be compiled in runtime

JasperReport jasperReport = JasperCompileManager.compileReport(your JRXMLReport path file here);
JasperReport subReport = JasperCompileManager.compileReport(your JRXMLSubReport path file here);

//you can notice that there're a lot of overloaded mathods to compile it, you can choose
//what is better in your case

//if you want to use the complied file *.jasper
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(new File("filenameReport.jasper"));

JasperReport subReport = (JasperReport)JRLoader.loadObject(new File("filenameSubReport.jasper"));


//Then you add your subReport in your parameter Map
parameters.put("SUBREPORT_JASPER_FILE", subReport);

JasperFillManager.fillReport(jasperReport, parameters,
                new JRBeanCollectionDataSource(yourList));

JRXML:

<parameter name="SUBREPORT_JASPER_FILE" class="java.lang.Object"/> // you can also create this using the option in your iReport


<subreport>
    <reportElement uuid="c62c7896-f37a-4e76-a4cb-70e2545947da" style="Style" x="279" y="0" width="296" height="15"/>
       <dataSourceExpression>
            <![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{yourSubList})]]>  
       </dataSourceExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_JASPER_FILE}]]></subreportExpression>
</subreport>

I think that way you'll accomplish what you're trying to do

查看更多
登录 后发表回答