Java:空比较总是产生错误:警告不正确?

发布于 2024-10-19 17:00:53 字数 1076 浏览 2 评论 0原文

有趣的情况。我有一段代码创建了多个 ZipOutputStreams。作为一项安全检查,在我考虑编写任何内容之前,我检查我的输出流是否已正确初始化:

ZipOutputStream countStream = null;
File countFile = null;
// other files

try {
    countFile =
    new File(savePath.getCanonicalPath() + savePath.separator + outputTag
        + "_COUNT_OUTPUTS.zip");
    // other files
} catch (Exception e) {
    outputLog.add("UNABLE TO FIND SAVE PATH");
    return util.FILE_INVALID;
}

try {
    // prepare outputs
    if (countFile.exists() == true) {
    countFile.delete();
    } else {
    }
    countStream = new ZipOutputStream(new FileOutputStream(countFile));
    // other files
} catch (Exception e) {
    e.printStackTrace();
    outputLog.add("UNABLE TO CREATE OUTPUT FILES");
    return util.FILE_SAVE_FAIL;
}

if (countStream == null) {
    outputLog.add("UNABLE TO CREATE OUTPUT FILES");
    return util.FILE_SAVE_FAIL;
} else {
}

我没有特别看到此代码中的问题出在哪里,但它会在空测试中发出警告:“空比较总是产生false:变量 countStream 在此位置不能为 null”。据我所知,我有一个变量初始化为 null,然后尝试创建输出流,但不能保证会发生。忽略警告很容易,但我更想知道编译器如何得出保证成功创建 countStream 的结论

K.Barad

Interesting situation. I have a section of code that creates several ZipOutputStreams. As a safety check before I even think about writing anything I check thta my output streams have been correctly initialised:

ZipOutputStream countStream = null;
File countFile = null;
// other files

try {
    countFile =
    new File(savePath.getCanonicalPath() + savePath.separator + outputTag
        + "_COUNT_OUTPUTS.zip");
    // other files
} catch (Exception e) {
    outputLog.add("UNABLE TO FIND SAVE PATH");
    return util.FILE_INVALID;
}

try {
    // prepare outputs
    if (countFile.exists() == true) {
    countFile.delete();
    } else {
    }
    countStream = new ZipOutputStream(new FileOutputStream(countFile));
    // other files
} catch (Exception e) {
    e.printStackTrace();
    outputLog.add("UNABLE TO CREATE OUTPUT FILES");
    return util.FILE_SAVE_FAIL;
}

if (countStream == null) {
    outputLog.add("UNABLE TO CREATE OUTPUT FILES");
    return util.FILE_SAVE_FAIL;
} else {
}

I dont especially see where the problem is in this code, but it throws up a warning on the null test: "Null comparison always yields false: The variable countStream cannot be null at this location". As far as I can see it, I have a variable initialised to null, then the creation of an outputstream is attempted, but isn't guaranteed to happen. ignoring the warning is easy enough, but I'd rather know how the compiler comes to the conclusion that countStream is guaranteed to be successfully created

K.Barad

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

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

发布评论

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

评论(3

唔猫 2024-10-26 17:00:53

编译器能够看到您不能为 null,因为:

  1. 在您的 try 中您已经初始化了它 (countStream = new ....) 并
  2. 在您的 >catch 你已经“return”退出该方法。

因此,代码“if (countStream == null)”只能在 countStream 初始化为非 null ZipOutputStream.

The compiler is able to see that you cannot be null there since:

  1. in your try you've initialized it (countStream = new ....) and
  2. in your catch you've "return"ed out of the method.

Hence, the code "if (countStream == null)" can only be attained through normal flow after countStream has been initialized to a non-null ZipOutputStream.

陌伤ぢ 2024-10-26 17:00:53

当没有异常时,变量countStream不会为null。当出现异常时,您返回 util.FILE_SAVE_FAIL 并且不会执行 null 检查。因此,每当执行空检查时,结果将始终为假。

稍微正式一点:没有任何代码路径可以让执行到达空检查,从而在执行检查时导致 countStream 为空。

When there is no Exception, the variable countStream will not be null. When there is an Exception, you return util.FILE_SAVE_FAIL and the null check will not be performed. Hence, whenever the null check is performed, the result will always be false.

Slightly more formal: There are no code paths by which execution can reach your null check that result in countStream being null when the check is performed.

最初的梦 2024-10-26 17:00:53

我从这段代码中了解到的是 countStream 为 null,这就是为什么它发出警告“Null 比较总是产生 false”

如果你调试,你会发现这段代码总是转到其他部分,因为 if (countStream == null) {< /code> 条件未通过。

What I understood from this code is countStream is null and thats why its giving warning that "Null comparison always yields false"

If you debug you will find this code always goes to else part because if (countStream == null) { condition does not pass.

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