空检查时可能尚未初始化错误 [英] Might not have been initialized error at null check
本文介绍了空检查时可能尚未初始化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在检查变量是否已初始化,但是那时netbeans给了我variable reader might not have been initialized
警告.如何解决/抑制这个问题?
I'm checking if the variable is initialized but at that point netbeans is giving me variable reader might not have been initialized
warning. How do I fix/suppress this?
这是我的代码(摘要):
This is my code (summary):
final Reader reader;
try {
reader = new Reader(directory);
//additional stuff that can cause an exception
} catch (Exception ex) {
//do stuff
} finally {
if (reader != null);
}
if检查的重点是确定是否已初始化.
The point of the if check is to determine whether it is initialized.
最佳做法是什么?
推荐答案
如果从未初始化reader
,则甚至没有null值.
If reader
was never initialized, it doesn't even have a null value.
更改
final Reader reader;
到
Reader reader = null;
确保它具有初始值.
这样,如果reader = new Reader(directory);
引发异常,则在经过finally块测试时,reader
将包含null
.
This way, if reader = new Reader(directory);
throws an exception, reader
will contain null
when tested by the finally block.
这篇关于空检查时可能尚未初始化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文