簡體   English   中英

嘗試修改整數時,它會引發NullPointer異常

[英]Integer throws NullPointer exception when trying to modify it

我正在嘗試根據通過使用Apache PDFBox通過對象傳遞的數據生成PDF。 由於數據量可變,因此我使用一個稱為volY的變量來跟蹤y位置以寫入信息。 如果volY大於700,我將關閉正在寫入的內容流,生成一個新頁面和一個新的內容流,然后開始寫入新頁面。 我用於寫入pdf的方法返回一個整數,該整數表示字符串的高度,並將其添加到volY 由於某些原因,當我嘗試遍歷元素時訪問volY時,會得到一個空指針異常。

這是我生成PDF的代碼:

public void generateSection(PDPage startingPage, PDPageContentStream cs, List<TestSection> sections) throws IOException {
    /*
     * TODO
     * verify list sequence integrity and reorder if not valid.
     */

    int volY = 350;
    PDPage page = startingPage;
    PDPageContentStream vcs = cs;
    // Iterate through sections
    for(int i = 0; i < sections.size(); i++) {
        if(volY > 700) {

            vcs.close();
            page = createPage();
            vcs = new PDPageContentStream(pd, page);
            volY = 50;
        }
        if(sections.get(i).isUrgent())
            cs.setNonStrokingColor(URGENT);

        drawString(sections.get(i).getType().getName(), 60, volY, vcs, page, 18);
        cs.setNonStrokingColor(REGULAR_TEXT);

        drawLine(vcs, 60, flipY(page, volY+8), 560);
        volY += 30;
        // Iterate through Items in section
        TestSection s = sections.get(i);
        for(int y = 0; y < s.getElements().size(); y++ ) {
            TestReportElement re = s.getElements().get(y);
            TestSubSection subSection = (TestSubSection)re;

            volY++;
            drawHeader(re.getTitle(), "a", volY, page, vcs);

            for(int z = 0; z < subSection.getItems().size(); z++) {
                //volY doesn't exist here for some reason?  At the very least it's not modifiable.


                if(vcs == null) {
                    System.err.println("VCS IS NULL");
                    System.exit(3);
                }
                TestInspectionItem ti = subSection.getItems().get(z);
                vcs.setNonStrokingColor(BOLD_TEXT);
                System.out.println(volY);
                drawMultipleStrings(ti.getPrompt(), volY, vcs, page, z);
                vcs.setNonStrokingColor(REGULAR_TEXT);
                for(int z1 = 0; z1 < ti.getResponses().size(); z1++) {
                    if (volY > 700) {

                        vcs.close();
                        page = createPage();
                        vcs = new PDPageContentStream(pd, page);
                        volY = 50;
                    }
                    String text = ti.getResponses().get(z1);
                    drawMultipleStrings(text, volY+15, vcs, page, z1);
                }
                if (volY > 700) {

                    vcs.close();
                    page = createPage();
                    vcs = new PDPageContentStream(pd, page);
                    volY = 50;
                }
            }

            if (volY > 700) {

                vcs.close();
                page = createPage();
                vcs = new PDPageContentStream(pd, page);
                volY = 50;
            }

        }
        // Add 70 to account for a new section.
        volY += 70;
    }
    vcs.close();
}

這是我用來繪制多個字符串的代碼:

private int drawMultipleStrings(String text, int y, PDPageContentStream cs, PDPage page, int index) {
    // Page is 900 units wide
    // Assume font size is 13
    int strSize = text.length();
    int height = 0;
    String textVal = text;
    List<String> allText = new ArrayList<>();
    int xVal = index % 2 == 0 ? 60 : 300;
    if(strSize > 40) {
        while(textVal.length() > 40) {
            for (int i = 40; i > 0; i--) {
                if (textVal.charAt(i) == ' ') {
                    allText.add(textVal.substring(0, i));
                    textVal = textVal.substring(i);
                    break;
                }
            }
        }
        allText.add(textVal);
        for(int ind = 0; ind < allText.size(); ind++) {
            String s = allText.get(ind);
            if(s.charAt(0) == ' ') {
                s = s.substring(1);
                drawString(s, xVal, y+(13*ind), cs, page, 13);
            } else {
                // This should only trigger on the first iteration.
                drawString(s, xVal, y+(13*ind), cs, page, 13);
            }
            height += 13;
        }
        // Allows items to be displayed in 2 columns based on the index
        return index % 2 == 0 ? 0 : height + 32;
    } else {
        drawString(text, index % 2 == 0 ? 60: 300, y, cs, page, 13);
        return 13;
    }
}

這段代碼可以正常工作,但是如果我更改drawMultipleStrings(ti.getPrompt(), volY, vcs, page, z); volY += drawMultipleStrings(ti.getPrompt(), volY, vcs, page, z); 它引發以下異常:

java.lang.NullPointerException
    at org.apache.pdfbox.pdmodel.PDPageContentStream.writeOperand(PDPageContentStream.java:2429)
    at org.apache.pdfbox.pdmodel.PDPageContentStream.setNonStrokingColor(PDPageContentStream.java:1316)
    at org.apache.pdfbox.pdmodel.PDPageContentStream.setNonStrokingColor(PDPageContentStream.java:1348)
    at compliancego.report.PdfService.generateSection(PdfService.java:206)
    at compliancego.report.PdfService.generateHeader(PdfService.java:176)
    at compliancego.report.PdfService.<init>(PdfService.java:97)
    at compliancego.report.PdfService.main(PdfService.java:73)

起初我以為這是因為不存在要寫入的一些數據,但是如果我不更新volY,它就可以正常工作。 然后,我認為這是創建新頁面但流存在時未創建PDPageContentStream的問題。

在此先感謝您的幫助!

最初,您創建一個本地頁面內容流變量,並使用接收到的頁面內容流作為參數進行初始化:

PDPageContentStream vcs = cs;

更改頁面后,您將關閉vcs的當前頁面內容流,然后將vcs設置為新頁面上的新流:

if (volY > 700) {

    vcs.close();
    page = createPage();
    vcs = new PDPageContentStream(pd, page);
    volY = 50;
}

但是在循環的sections代碼中,您使用的是cs而不是vcs

cs.setNonStrokingColor(URGENT);
...
cs.setNonStrokingColor(REGULAR_TEXT);

在第一頁更改期間,您關閉了vcs ,這些vcs指向與cs相同的流。 因此,這些顏色設置指令繪制在封閉的流cs ,這導致觀察到的NullPointerException

要解決此問題,也可以在此處使用vcs而不是cs


僅在您更改后才發生這種情況的原因

drawMultipleStrings(ti.getPrompt(), volY, vcs, page, z);

volY += drawMultipleStrings(ti.getPrompt(), volY, vcs, page, z);

最有可能的是,在此更改之前, volY從未增長到足以觸發頁面更改的大小,但此后確實如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM