当前位置: 首页 > 知识库问答 >
问题:

Integer在尝试修改时引发NullPointer异常

祁雪峰
2023-03-14

下面是我生成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。

提前感谢您的帮助!

共有1个答案

夔修伟
2023-03-14

您首先创建一个本地页面内容流变量,并将其作为一个参数进行初始化:

PDPageContentStream vcs = cs;

页面更改后,关闭VCS中的当前页面内容流,然后将VCS设置为新页面上的新流:

if (volY > 700) {

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

但在循环的两个代码行中,您使用的是cs而不是vcs:

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

在第一次页面更改期间,您关闭了vcs,它指向与cs相同的流。因此,这些颜色设置指令被绘制在闭合流cs上,这导致观察到的NullPointerException

要解决此问题,请在此处使用vcs而不是cs

这只发生在你改变后的原因

drawMultipleStrings(ti.getPrompt(), volY, vcs, page, z);
volY += drawMultipleStrings(ti.getPrompt(), volY, vcs, page, z);
 类似资料:
  • 我尝试了很多方法在片段视图中成功实现了onitemselectedlistener,但不断地得到一个致命的java异常。它始终指向代码中的同一行,用于为spinner设置onitemselectedlistener方法。 android studio logcat错误输出为: 07-21 13:55:06.544 17277-17277/com。vaibhavtech。indoreveg E/An

  • 问题内容: 我正在研究一个第三方开发人员用来为我们的核心应用程序编写扩展的Python库。 我想知道引发异常时是否可以修改回溯,因此最后一个堆栈帧是对开发人员代码中库函数的调用,而不是对引发异常的库中的行的调用。堆栈底部还有一些框架,其中包含对第一次加载我理想上也希望删除的代码时使用的函数的引用。 在此先感谢您的任何建议! 问题答案: 不更改回溯怎么办?您要求的两件事都可以通过不同的方式轻松完成。

  • 有一个列表正在被两个线程同时排序和迭代。不出所料,它导致。不清楚的是错误的时间。 输出:(出现几次) 为什么当第一个线程完成时,另一个线程的迭代失败? 当两个线程分别完成排序并获得迭代器时,底层迭代器将不会改变。为什么在此阶段会导致异常?

  • 我正在尝试从我的android应用程序(目标5.0)播放. opus媒体文件。此文件的来源是设备外部存储。 MediaPlayer继续抛出“java.io.IOException:Prepare failed.:status=0x1”。请注意,我可以播放其他文件类型,如mp3和aac 注意:已授予READ\u EXTERNAL\u存储权限。 提前谢谢你。 下面是我在Android片段中的部分代码:

  • 当我使用temp=iterator.next()时,sort方法会导致并发修改错误。你能帮我解决并发修改错误吗。我给出了整个类的代码,但我只是尝试完成sort方法。事先谢谢你的帮助。 我必须对ArrayList中的所有数组进行排序。