空解除引用强化问题

发布于 2025-01-15 03:51:39 字数 1033 浏览 2 评论 0原文

我正在努力修复代码库中的强化发现,扫描返回空取消引用发现。建议我向 csvWriter 添加空检查,但说实话我并不完全知道这意味着什么。有人能指出我正确的方向吗?谢谢。

    private void writeBadRecordFile() {

        CsvWriter csvWriter = null;

        try {
            badRecordFile = File.createTempFile("BadRecords", ".csv");
            badRecordFile.deleteOnExit();

                FileWriter writer = new FileWriter(badRecordFile);

                csvWriter = new CsvWriter(writer, ',');
                String[] header = {"BIC"};

                csvWriter.writeRecord(header);

                for (String badRecord : badRecords) {

                    csvWriter.write(badRecord);
                    csvWriter.endRecord();
                }

                csvWriter.flush();

            } catch (IOException e) {
                errorMessage = "Bad records were detected in the uploaded file, but the bad record file was not able to be created.";
                e.printStackTrace();

            } finally {

                csvWriter.close();
            }
        }

I'm working on remediating fortify findings within a code base and the scan came back with a null dereference finding. The recommendation is that I add a null check to csvWriter, but to be honest I don't exactly know what that means. Could anybody point me in the right direction? Thanks.

    private void writeBadRecordFile() {

        CsvWriter csvWriter = null;

        try {
            badRecordFile = File.createTempFile("BadRecords", ".csv");
            badRecordFile.deleteOnExit();

                FileWriter writer = new FileWriter(badRecordFile);

                csvWriter = new CsvWriter(writer, ',');
                String[] header = {"BIC"};

                csvWriter.writeRecord(header);

                for (String badRecord : badRecords) {

                    csvWriter.write(badRecord);
                    csvWriter.endRecord();
                }

                csvWriter.flush();

            } catch (IOException e) {
                errorMessage = "Bad records were detected in the uploaded file, but the bad record file was not able to be created.";
                e.printStackTrace();

            } finally {

                csvWriter.close();
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拔了角的鹿 2025-01-22 03:51:39

如果 try 块的前三行失败,则 csvWriter 仍为 null,并且 finally 块运行 csvWriter.close 将抛出 NullPointerException。 linter 建议您

if (csvWriter != null) {
  csvWriter.close();
}

finally 块中执行此操作。或者,更好的是,使用 Java 的 try-with-resources 语句完全放弃这个问题。

If the first three lines of your try block fail, then csvWriter is still null, and the finally block which runs csvWriter.close will throw NullPointerException. The linter is recommending that you do

if (csvWriter != null) {
  csvWriter.close();
}

in your finally block. Or, better, use Java's try-with-resources statement to forgo the problem entirely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文