Monim
Monim

Reputation: 3577

ArrayOutOfBoundException when exporting jasperreport using JRTextExporter

I'm using jasper reports library to add some jasper reports to my java web application, the report itself is fine, but the problem comes when I try to export it from my application using the bellow code

    JasperPrint jasperPrint;
try {
    //get the database connection
    Connection connection = dataSource.getConnection();
    FileInputStream fin = new FileInputStream(report_path);
    ObjectInputStream ois = new ObjectInputStream(fin);
    jasperReport = (JasperReport) ois.readObject();
    ois.close();
    //get report result
    jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
    connection.close();
    if (jasperPrint.getPages().size() > 0) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JRExporter exporter;
        exporter = new JRTextExporter();
        exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT,19f);
        exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH,7f);
        exporter.setParameter(JRTextExporterParameter.BETWEEN_PAGES_TEXT,"");
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
        exporter.exportReport();
    }
}
catch (Exception e) {
    e.printStackTrace();
}

The following error occurs when I try to export the report

java.lang.ArrayIndexOutOfBoundsException: 12
at net.sf.jasperreports.engine.export.JRTextExporter.exportText(JRTextExporter.java:614)
at net.sf.jasperreports.engine.export.JRTextExporter.exportElements(JRTextExporter.java:392)
at net.sf.jasperreports.engine.export.JRTextExporter.exportPage(JRTextExporter.java:369)
at net.sf.jasperreports.engine.export.JRTextExporter.exportReportToWriter(JRTextExporter.java:345)
at net.sf.jasperreports.engine.export.JRTextExporter.exportReport(JRTextExporter.java:194)

The line causing the error is this one exporter.exportReport();

The weird thing about this is that when I choose any other format the code runs fine and I get the report, also for text format if the data is less than 5 lines the report exports successfully. I searched this and found it was a bug in the jasperreports library and should be fixed in version 2.2.1 I think, I'm using jasperreports library version 5.5.1 I also used version 2.2.1 and 6.x but the problem is still there.
It seems like it have something to do with the page width and height, but I could not know how to solve it. This is the first line of my report

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report1" language="groovy" printOrder="Horizontal" pageWidth="1875" pageHeight="40" orientation="Landscape" columnWidth="1875" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" isIgnorePagination="true" uuid="b8c06aba-6b16-4020-8a9e-574d630cdbae">

Also I observed that when I set isIgnorePagination="false" the problem goes but I have empty lines at the end of the report, which I don't want.
please help, let me know the cause of the error and the fix of it if there is.

Upvotes: 2

Views: 1845

Answers (2)

In my case it occurred this problem in version 4.5.1 of JasperReport.

I was using subreports to generate TXT file.

To fix it was necessary to use the same bandwidth and the width of the textField in all sub reports. In my example was the width of the band 15 and the width of the textField 14.

Upvotes: 0

Sathya
Sathya

Reputation: 51

Some how I resolved same kind issue with my Japser Text Exporter, the issue was like below,

I am using Sub reports and the tag inside it is as below with x and y negative value because while aligning it in design mode it has to be placed out of the boundary to get proper aligned report with main,

<reportElement stretchType="RelativeToBandHeight" x="-6" y="-1" width="54" height="13"

and after that there is a recordCounter in sub report and when it crosses 99 and 100 is three characters, that time I get this exception from exportText method JRTextExporter class, then I increased the width recordCounter to fit in 5 characters and now it exporting fine.

It might not be the same in your case, but just thought of sharing my isue n fix.

I am using jasperreports-6.2.0

Upvotes: 1

Related Questions