Jump to content
Changes to the Jaspersoft community edition download ×

URGENT:Can someone help me in subreports.PLZ.

2005 IR Help

Recommended Posts

By: racz - raczcandy

URGENT:Can someone help me in subreports.PLZ.

2004-05-17 10:50

i'm a beginner in using jasper reports. What are the steps in creating a subreport. Well, i've created a master report and a sub... I used a java class to compile, fillreport and preview it. But i can only preview the master report and it does not contain the data in the sub... How can i link the master and the subreport? please reply to my post. I need it badly... Thank u so much... =)





By: C-Box - c-box

RE: URGENT:Can someone help me in subreports.PLZ.

2004-05-17 12:44



try to load the SubReport from XML/InputStream then compile it to a JasperReportObject and put that JasperReportObject into the Parameter-HashMap then use in your MasterReports SubReportSection the given SubReportParameter as SubReportExpression and call the fill-Method for the MasterReport with the ParameterMap and it's DataSource/Connection that's it.








By: racz - raczcandy

RE: URGENT:Can someone help me in subreports.PLZ.

2004-05-17 16:43

I use this code to compile, filll and preview my reports: Is it correct? can i view my sub reports using these codes?



public class ClosedAccountsReport {



private static final String reportsPath = "D:\MCDS REPORTS\Templates\";

private ClosedAccountsReportVO closedAccountsReport = new ClosedAccountsReportVO();

private static ArrayList dataList = new ArrayList();

private static Map parameters = new HashMap();

private static Map parametersMain = new HashMap();


public ClosedAccountsReport() {




void prepareDataSub() {


closedAccountsReport.setRunDate(new Date());

closedAccountsReport.setRunTime(new Date());




closedAccountsReport.setPrintDate(new Date());



closedAccountsReport.setAccountNumber(new Long (834000012));


closedAccountsReport.setClosureAmount(new Double(120000.01));

closedAccountsReport.setCharges(new Double(1000.55));



















void fillReport(String reportName) {


try {

//for subreports

JasperPrint jrPrint = JasperFillManager.fillReport(reportsPath + reportName + ".jasper", parameters, new JRBeanCollectionDataSource(dataList));


catch(JRException e) {

System.out.println("JRException : " + e.getMessage());





void prepareDataMain() {

parametersMain.put("SUBReport", parameters);





void compileReport(String reportName) {

try {

// compile XML report template

// output file extension is .jasper

JasperCompileManager.compileReportToFile(reportsPath + reportName + ".xml",reportsPath + reportName + ".jasper");


catch(JRException e) {

System.out.println("JRException : " + e.getMessage());







void previewReport(String reportName) {


try {

// fill compiled report template with data

JasperPrint jrPrint = JasperFillManager.fillReport(reportsPath + reportName + ".jasper", parametersMain, new JRBeanCollectionDataSource(dataList));

// open JasperViewer

JasperViewer.viewReport(jrPrint, true);


catch(JRException e) {

System.out.println("JRException : " + e.getMessage());





public static void main(String[] args) {

ClosedAccountsReportRM reportsManager = new ClosedAccountsReportRM();












there's an error... what caused it?


dori.jasper.engine.JRException: Incompatible value assigned to parameter SUBReport : Untitled_report_1


at dori.jasper.engine.fill.JRBaseFiller.setParameter(JRBaseFiller.java:680)


at dori.jasper.engine.fill.JRBaseFiller.setParameters(JRBaseFiller.java:644)


at dori.jasper.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:522)


JRException : Incompatible value assigned to parameter SUBReport : Untitled_report_1


at dori.jasper.engine.fill.JRFiller.fillReport(JRFiller.java:159)


at dori.jasper.engine.JasperFillManager.fillReport(JasperFillManager.java:350)


at dori.jasper.engine.JasperFillManager.fillReport(JasperFillManager.java:290)


at mcdsreports.ClosedAccountsReportRM.previewReport(ClosedAccountsReportRM.java:142)


at mcdsreports.ClosedAccountsReport.main(ClosedAccountsReport.java:120)



Thanks! i need a reply asap... Thank u so much =)





By: C-Box - c-box

RE: URGENT:Can someone help me in subreports.PLZ.

2004-05-18 02:22

You put the SubReport as type "HashMap" into the parameter-map..... that's false.


You needn't fill your SubReport. Instead just load the SubReport and compile it to a "JasperReport" Object... than put that object into the parametersMain when filling MasterReport.




JasperPrint jasperPrint = new JasperPrint();

String reportFileName = "O:/Java Projects/JasperSource/reports/SQL/mr_afpsangebot.xml";

File myReportFile = new File(reportFileName);//MasterReport as File

String repDir = myReportFile.getParent();// Directory for the report(s)

File mySubRepPositions = new File(repDir, "SR_AFPSAngebotPos.xml");// first SubReport

File mySubRepSeriennummern = new File(repDir, "SR_AFPSAngebotPosSNr.xml");// nested second SubReport (it's within SubRepPositions)


JasperCompileManager MyJC = new JasperCompileManager();

JasperReport subRepPos = null;

subRepPos = MyJC.compileReport(getInputStream(mySubRepPositions)); //own Method "getInputStream" that gets the report as inputstream

JasperReport subRepSNr = null;

subRepSNr = MyJC.compileReport(getInputStream(mySubRepSeriennummern)); //own Method "getInputStream" that gets the report as inputstream

JasperReport masterRep = MyJC.compileReport(getInputStream(reportFileName));


Map parameters = new HashMap();

parameters.put("SRPositionen", subRepPos); // put here the first SubReport

parameters.put("SRSeriennummern", subRepSNr); // put here the second SubReport

parameters.put("DSPositionen", new JREmptyDataSource());// just for Test with empty DataSources replace it with own DataSourceModel

parameters.put("DSSeriennummern", new JREmptyDataSource()); // just for Test with empty DataSources replace it with own DataSourceModel



JasperFillManager MyJFM = new JasperFillManager();

jasperPrint = MyJFM.fillReport(masterRep, parameters, getDBConnection()); // fill the MasterReport


JasperViewer jasperViewer = new JasperViewer(jasperPrint,false);


jasperViewer.show(); // view the whole Report with two SubReports (even one is in the other --> "nested" SubReport)


Perhaps that could help you!?!?!?








By: racz - raczcandy

RE: URGENT:Can someone help me in subreports.PLZ.

2004-05-18 07:32

but i don't get my data directly from a db... i get the data from bean... is you example still applicable? how can i put the values from my bean if i don't put it in a hashMap? how can i merge my subreport and master report? is it at design time using iReport or at runtime. Thanks! i've got a deadlyn about this that's why i need it badly and got to understand it.

Thanks for you help... =)





By: C-Box - c-box

RE: URGENT:Can someone help me in subreports.PLZ.

2004-05-18 23:51

I also don't use direct database-connection...


I use JRTabelModelDataSource for all my (sub)reports.

You can use my modell for your purpose also.

Just fill your JRBeanArrayDataSource/JRBeanCollectionDataSource with your data and put it also into the hashmap when filling your masterReport.

As far as I know it's neccessarry that every SubReport uses it's own DataSource that's why you have to put it into your parameter's map, in order the SubReport knows where it has to take the data from.




parameters.put("DSPositionen", getMyJRDataSource("DSPositionen"));// here you write yourself a method that returns a JRDataSource-Object that contains all your data that should be shown by SubReport1


parameters.put("DSSeriennummern", getMyJRDataSource("DSSerienummern")); // here you write yourself a method that returns a JRDataSource-Object that contains all your data that should be shown by SubReport2


here a sample like I do for TestPurpose:


public JRDataSource getMyJRDataSource(String pName){

JRDataSource reportDaBa = new JREmptyDataSource();

if (pName != null){

if (pName.equalsIgnoreCase("DSPositionen")){

String[] columnNames = {"ID","Nr","Datum", "Firma1", "Firma2","Firma3"};

Object[][] data = {

{new Integer(1),"A040001","01.01.2004","MyFirm1","MyFirm2","MyFirm3"},


reportDaBa = new JRTableModelDataSource(new PrintDatasource(data,columnNames));

}else if(pName.equalsIgnoreCase("DSSerienummern")){

String[] columnNames = {"ID","Serienummer","SNDatum", "SNBemerkung"};

Object[][] data = {

{new Integer(13),"SNAUDIA4-000000001","24.12.2003","Bem1"},

{new Integer(13),"SNAUDIA4-000000002","31.12.2003","Bem2"},

{new Integer(22),"SNBMW525-525000001","05.05.2003","BMW Bem3"},

{new Integer(22),"SNBMW525-525000002","15.05.2003","BMW Bem4"},

{new Integer(22),"SNBMW525-525000003","16.06.2003",""},


reportDaBa = new JRTableModelDataSource(new PrintDatasource(data,columnNames));



return reportDaBa;



you could write your own method in that way and fill your data from your beans/dto's.


I experienced some trouble when building a relationship between Master&Detail using SubReports, because with JRDataSources (no matter if JRTableModelDataSource or JRBeanCollectionDataSource) there is no connection given.


But therefore you can search that forum, there are some workarounds posted.




Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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...