Jump to content
We've recently updated our Privacy Statement, available here ×

subreport error


2004 IR Help

Recommended Posts

By: morphZion - morphzion

subreport error

2005-03-14 04:37

hi,

is there anyone know how to use the subreport without using connection? my data is pass in thru the data object. i tried, but the subreport doesn't show up. something seems missing.

 

pls tell me why? n how to do it.

 

 

 

 

 

By: mbobbitt - mbobbitt

RE: subreport error

2005-03-15 07:04

I don't think subreports work correctly, especially when trying to use them in a web application. I have found no one who can help me either. I've found that support/documentation for new users is discouraging at best, Jasper has been a real disappointment for me.

 

 

 

 

By: charmadi - charmadi

RE: subreport error

2005-03-15 14:38

I am not how you are trying to do but I did not have any problem using subreports using my custom data source.

 

This is what you need to do.

 

Create two parameters in your main report

 

<parameter name="dataSource" isForPrompting="false" class="net.sf.jasperreports.engine.JRDataSource"/>

<parameter name="reportName" isForPrompting="false" class="net.sf.jasperreports.engine.JasperReport"/>

 

before calling fillReport() put the above paramters in the java.util.Map and pass it as argument.

 

Map map = new HashMap();

map.put("dataSource", yourDataSouce);

 

Object reportObj = JRLoader.loadObject("subreport.jasper") to load your subreport

 

map.put("reportName", reportObj);

 

then call JasperFillManager.fillReport() passing map and other parameter needed.

 

 

Hope this helps.

 

 

 

 

 

 

By: Brad Balmer - bbalmer

RE: subreport error

2005-03-16 11:03

But how would your master report get the correct subreport data?

 

For example, my main report is a collection of beans. My header of the main report consists of data from one of the beans and I want to pull an ArrayList of beans to display for the subreport for each header bean based on a particular attribute of the header bean (rteId).

 

Therefore, I passin my JRDataSource when I call runReportToPdf. Do I now put in the HashTable (passed to the runReportToPDF) simply my subreport JRDataSource? I guess I don't understand how the header of the main report will pull the correct data for the subreport.

 

Do I create a HashTable using the rteId as the key and the JRDataSource as the value and pass this HashTable to the main report?

 

Thanks and sorry for the rambling.

 

 

 

 

By: charmadi - charmadi

RE: subreport error

2005-03-16 16:57

I might not have been very clear in my previous posting or I may be misreading your posting.

 

For every subreport you have in your main report create two parameters, one of type datasource (you can put your custom datasource) and second one of JasperReport.

 

And in subreport element of the main jasper report use those parameters. And before calling runReportToPDF put the data source and jasper reports in the hash map and main report will pass the correct data source to the subreport.

 

If you look your main report xml file it should look something like this. I have not posted complete file but this should give you an idea.

 

<parameter name="SubReportDataSource" isForPrompting="false" class="net.sf.jasperreports.engine.JRDataSource"/>

<parameter name="SubReportName" isForPrompting="false" class="net.sf.jasperreports.engine.JasperReport"/>

 

 

<subreport isUsingCache="true">

<reportElement ..../>

<dataSourceExpression><![CDATA[$P{SubReportDataSource}]]></dataSourceExpression>

<subreportExpression class="net.sf.jasperreports.engine.JasperReport">

<![CDATA[$P{SubReportName}]]></subreportExpression>

</subreport>

 

If you continue to have problem send me a email.

 

 

Hope this helps.

 

 

 

 

 

By: morphZion - morphzion

RE: subreport error

2005-03-16 18:02

hi charmadi,

why the datasource object must stored into map first before fill report? if i din stored into the map, it does not work.

 

this is how i test,

case 1:

1. a method call getJRParameter(HashMap data), return as map object. //note: wothout store the datasource object. just appropriate data need to display in the report.

 

2. a method call getDataSource(HashMap data), return as JRDataSource object.

 

3. load the jasper object the fill up the report with appropriate data and export to pdf format. below is the code,

 

try {

if (connection == null) {

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, getJRParameter(data), getDataSource(data));

 

JasperExportManager.exportReportToPdfFile(jasperPrint, outputFile);

 

} else {

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, getJRParameter(data), connection);

JasperExportManager.exportReportToPdfFile(jasperPrint, outputFile);

 

}

} catch (JRException exp) {

throw new ReportGenerationException(exp);

 

this way of working does not display the subreport. but in the code i already pass in the datasource object. why cannot? i dun understand this part.

 

case 2:

1. a method call getJRParameter(HashMap data), return as map object.

//note: this time store the datasource object into map by adding the code : map.put("dataSource", getDataSource(data));

 

the "dataSource" is match with the parameter name in main jasper report.

 

2. same as above.

 

3. same as above.

 

by doing this way, it work. Same question here, Charmadi, can you tell me why the data source object must store into map?

 

thanks a lot.

 

 

 

 

By: charmadi - charmadi

RE: subreport error

2005-03-17 16:16

Hi,

 

I pressume this is what happens in the code. I haven't had the time to go thru Jasper source. In the main report when it goes thru the list of subreports and looks for "dataSourceExpression" and "subReportExpression". And for each "subReportExpression " it looks for that paramter in the parameter list that is passed to the master report and if found loads the the report to the memory and looks for the datasouce in the paramter list and if found calls fillReport passing subreport and its data source.

 

Question is are you using the same datasource for the master and subreports? If that is what you want to use then you can use predefined datasouce ($P{REPORT_DATASOURCE}parameter of the master report. If you are using the same datasource and sub report you need to extend/implement your datasource from JRRewindableDataSource.

 

In your getDataSource() function are you creating a new DataSource?

 

 

One more thing, getJRParameter(Map map) and getDataSource(Map map) are these your functions that fill in the parameters and returns datasouce that need to be passed to the master report?

 

 

 

I am not sure whether I have answered your question.

 

Let me know.

Link to comment
Share on other sites

  • 7 months later...
  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

i followed the instructions u mentioned but its not working.

 

Report containig master and subreport doesnt show up and gives "Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space" while filling the report.

 

Both master and sub reports use JRBeanCollectionDataSource as their data source.

 

Interestingly when i call the master report separately without it containg the sub reports, it run fine and also when i try to rub the sub report standalone it works also too.

 

The problem is when i integrate the sub report in master, it doesnt show up.

 

=======================================

***** Master report paramters *****

***********************************

<parameter name="subReportDataSource" isForPrompting="false" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>

<parameter name="subreportA" isForPrompting="false" class="net.sf.jasperreports.engine.JasperReport"/>

=======================================

 

=======================================

***** Subreport element in master report ****

*********************************************

<subreport isUsingCache="true"> <reportElement x="0" y="67" width="190" height="40" key="subreport-1"/> <dataSourceExpression><![CDATA[$P{subReportDataSource}]]></dataSourceExpression>

<subreportExpression class="net.sf.jasperreports.engine.JasperReport">

<![CDATA[$P{subreportA}]]></subreportExpression>

</subreport>

===============================================

 

=========================================

***** My java code ****************

***********************************

ArrayList miniStatement = new ArrayList();

ArrayList miniStatementDetail = new ArrayList();

 

MiniStatementBean customerBean = new

MiniStatementBean();

customerBean.setAccount(selectedAcctNumber);

 

MiniStatementDetailBean detailBean = new MiniStatementDetailBean();

detailBean.setTranDate("12/12/12");

detailBean.setTranName("abc");

detailBean.setAmount("12222");

 

miniStatementDetail.add(detailBean);

miniStatement.add(customerBean);

 

// Master Data source

JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(miniStatement);

// Subreport Data source

JRBeanCollectionDataSource dsDetail = new JRBeanCollectionDataSource(miniStatementDetail);

 

String subReport= "reports/ministatementdetail.jrxml";

String subReportFile= JasperCompileManager.compileReportToFile(subReport);

jasperSubReport=(JasperReport)JRLoader.loadObject(subReportFile);

jasperReport=JasperCompileManager.compileReport("reports/ministatement.jrxml");

 

Map paramters = new HashMap();

paramters.put("subReportDataSource", dsDetail);

paramters.put("subreportA", jasperSubReport);

 

jasperPrint=JasperFillManager.fillReport(jasperReport, paramters, ds);

 

JasperViewer.viewReport(jasperPrint, false);

 

=========================================

Link to comment
Share on other sites

  • 3 years later...

Ugh. 5 years later and I am still having the same problems as the original poster, and still no answer is posted.

charmadi posted a solution that only works when the content of the sub-report is unrelated to the main report data source, such as the content of a reference table.

However, the main use of a sub-report should be for child data of the main report. For example, if printing a contact list, the main report would list persons (one detail row per person), and the sub-report would be used to print phone numbers (zero to many rows depending on how many phone numbers each person has).

There is no way that the phone numbers data source can be set as suggested by charmadi, since the content of the data source needs to be sourced from the collection of phone numbers in the Person object in the main data source.

Does anyone have an example to post here to answer my question and to close off this 5 year old unsolved thread?

Thanks for any help.

Link to comment
Share on other sites

  • 2 weeks later...

e-sarge
Wrote:

Does anyone have an example to post here to answer my question and to close off this 5 year old unsolved thread?

I don't have a full example, but what you would need to do is to map the collection of phone numbers to a java.util.List field in the master report, and then use new JRBeanCollectionDataSource($F{phoneList}) as data source for the subreport.  If the elements in the list are Strings, you can map the value to a subreport field called _THIS.

Regards,

Lucian

Link to comment
Share on other sites

Thanks. I got it working (JRXML files attached below).

By the way, very important for me resolving this was to ensure I had the same version of the Jasper Reports JAR file in Tomcat that I was using in my designer (iReports). Mine was a different version, and it failed silently, only symptom was blank detail rows in the sub-report (but sub-report page and column headers worked).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...