线程“main”中的异常net.sf.jasperreports.engine.JRRuntimeException:页面索引超出范围:0 of -1

时间:2017-09-04 06:13:16

标签: java jasper-reports

我正在编写一个Java程序,它接受jrxml文件并编译jrxml,然后将报告导出为png格式的图像。
之后,我试图通过连接到我的桌面的斑马打印机打印该图像。但它在执行期间抛出net.sf.jasperreports.engine.JRRuntimeException异常。我不知道这个错误意味着什么。

错误日志

Exception in thread "main" net.sf.jasperreports.engine.JRRuntimeException: Page index out of range: 0 of -1.
    at net.sf.jasperreports.engine.JRAbstractExporter.getPageRange(JRAbstractExporter.java:804)
    at net.sf.jasperreports.engine.export.JRGraphics2DExporter.exportReportToGraphics2D(JRGraphics2DExporter.java:302)
    at net.sf.jasperreports.engine.export.JRGraphics2DExporter.exportReport(JRGraphics2DExporter.java:229)
    at net.sf.jasperreports.engine.print.JRPrinterAWT.printPageToImage(JRPrinterAWT.java:288)
    at net.sf.jasperreports.engine.JasperPrintManager.printToImage(JasperPrintManager.java:290)
at net.sf.jasperreports.engine.JasperPrintManager.printPageToImage(JasperPrintManager.java:446)
at com.greycode.optimization.Materialsin.generateReport(Materialsin.java:62)

完整的Java代码:

public class Materialsin {
    public static final void main(String[] args) throws FileNotFoundException {
        Materialsin report = new Materialsin();
        try {
            report.generateReport(null, null);
        } catch (JRException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (ZebraPrinterLanguageUnknownException e) {
            e.printStackTrace();
        }
    }

    static JasperReport jasperReport = null;
    public void generateReport(Map<String, Object> parameters, List<Label> labels)
            throws JRException, IOException, ConnectionException, ZebraPrinterLanguageUnknownException {
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(labels);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
        if (jasperPrint != null) {
            FileOutputStream fos = new FileOutputStream("C:\\Users\\optimization\\Labels.png");
            BufferedImage rendered_image = null;
            rendered_image = (BufferedImage) JasperPrintManager.printPageToImage(jasperPrint, 0, 1.6f);
            ImageIO.write(rendered_image, "png", fos);

            try {
                Connection thePrinterConn = new DriverPrinterConnection("GC420t",1000,1000);
                thePrinterConn.open();

                ZebraPrinter zPrinter = ZebraPrinterFactory.getInstance(thePrinterConn);
                PrinterStatus printerStatus = zPrinter.getCurrentStatus();
                if(printerStatus.isReadyToPrint) {
                    ZebraImageI image = ZebraImageFactory
                        .getImage("C:\\Users\\optimization\\Labels.png");
                } else {
                    System.out.println("Something went wrong");
                }
            } finally {
            }
        }
    }

    static {
        try {
            jasperReport = JasperCompileManager
                    .compileReport(new FileInputStream("C:\\Users\\optimization\\Label.jrxml"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JRException e) {
            e.printStackTrace();
        }
    }
}

任何人都可以帮我解决这个问题吗?

0 个答案:

没有答案