线程“main”net.sf.jasperreports.engine.jrruntimeexception中出现异常:页索引超出范围:第0个,共1个

zzwlnbp8  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(337)

我正在编写一个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();
        }
    }
}

有人能帮我修一下吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题