空解除引用强化问题
我正在努力修复代码库中的强化发现,扫描返回空取消引用发现。建议我向 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
try
块的前三行失败,则csvWriter
仍为null
,并且finally
块运行csvWriter.close
将抛出NullPointerException
。 linter 建议您在
finally
块中执行此操作。或者,更好的是,使用 Java 的 try-with-resources 语句完全放弃这个问题。If the first three lines of your
try
block fail, thencsvWriter
is stillnull
, and thefinally
block which runscsvWriter.close
will throwNullPointerException
. The linter is recommending that you doin your
finally
block. Or, better, use Java's try-with-resources statement to forgo the problem entirely.