[英]JAVA Warning - Dereferancing Possible Null Pointer. How do I properly get rid of this warning?
I am learning JAVA. 我正在学习JAVA。
I cannot seem to find a way to get rid of this 'possible null derefence' warning. 我似乎找不到消除这种“可能的null取消引用”警告的方法。 I have created fOut in the proper scope as null, otherwise I get a 'may not have been initialized' error.
我已经在适当的范围内将fOut创建为null,否则会出现“可能尚未初始化”错误。
I was unable to find an simple example that would help me fix this. 我无法找到一个简单的示例来帮助解决此问题。 I know it is probably a simple answer.
我知道这可能是一个简单的答案。
Thank you. 谢谢。
public static int waveToFile (String filename, byte[] byteWave)
{
FileOutputStream fOut = null;
File file = new File (filename);
try
{
fOut = new FileOutputStream(file);
}
catch (IOException e)
{
}
try{
fOut.write(byteWave); //THIS IS WARNING of POSSIBLE DE-REFERENCE
fOut.close(); //OF NULL POINTER
}
catch (IOException e)
{
}
return mErr;
}
If an exception is thrown fOut
can be null
. 如果抛出异常,则
fOut
可以为null
。 Therefore the compiler is warning you. 因此,编译器警告您。
To avoid it, check that it is not null
: 为了避免这种情况,请检查它是否不为
null
:
finally {
if(fOut != null) {
fOut.close();
}
}
As a side note: 附带说明:
close
in a finally block to make sure it is executed close
放在finally块中以确保执行 fOut
if there has been an exception fOut
You can also use a try-with-resources statement which is perfectly safe and does the work for you: 您还可以使用try-with-resources语句 ,该语句非常安全,可以为您完成工作:
try(fOut = new FileOutputStream(file)) {
fOut.write(byteWave);
} catch(IOException e) {
// do something with e
}
The fact is that you are constructing the file object and using it in two different try-catch blocks. 事实是您正在构造文件对象,并在两个不同的try-catch块中使用它。 So if this fails:
因此,如果失败:
fOut = new FileOutputStream(file);
no value will be assigned to fOut
, the first catch
will handle the exception and the excution will continue with: 没有将值分配给
fOut
,第一个catch
将处理异常,执行将继续执行:
fOut.write(byteWave); //THIS IS WARNING of POSSIBLE DE-REFERENCE
fOut.close(); //OF NULL POINTER
Hah! 哈哈!
fOut
is null
because the previous block failed. fOut
为null
因为前一个块失败。 The warning is well justified. 该警告是合理的。
First of all, wrap the two operations in the same try-catch block: 首先,将两个操作包装在同一try-catch块中:
FileOutputStream fOut = null;
File file = new File (filename);
try {
fOut = new FileOutputStream(file);
fOut.write(byteWave);
fOut.close();
}
catch (IOException e) {
// Do something
}
This piece of code has still a problem: if write
fails and an exception is thrown, close
will never be called. 这段代码仍然有一个问题:如果
write
失败并抛出异常,将永远不会调用close
。 You can use a finally
block to ensure that the file is closed no matter what or, better, a closing context: 您可以使用
finally
块来确保关闭文件,无论关闭什么上下文,或者更好的是关闭上下文:
File file = new File (filename);
try (FileOutputStream fOut = new FileOutputStream(file)){
fOut.write(byteWave);
}
catch (IOException e) {
// Do something
}
you need two try block. 您需要两个try块。
public static int waveToFile (String filename, byte[] byteWave)
{
FileOutputStream fOut = null;
File file = new File (filename);
try
{
fOut = new FileOutputStream(file);
fOut.write(byteWave);
if(fOut !=null){
fOut.close();
}
}
catch (IOException e)
{
}
return mErr;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.