当变量被声明为具有函数作用域时,我收到类型错误
以下是一些函数:
jTextField1.setEnabled(false);
jTextField2.setEnabled(false);
jTextField3.setEnabled(false);
jComboBox1.setEnabled(false);
jComboBox2.setEnabled(false);
String samplingRate = jTextField1.getText();
String sampleSize = jTextField2.getText();
String channels = jTextField3.getText();
String endian = (String)jComboBox1.getSelectedItem();
String outputFormat = (String)jComboBox2.getSelectedItem();
AudioFormat outputAudioFormat = new AudioFormat( Float.parseFloat(samplingRate) , Integer.parseInt(sampleSize) , Integer.parseInt(channels) , true , Boolean.parseBoolean(endian) );
AudioInputStream newAIS; // newAIS declared Here
try {
newAIS = AudioSystem.getAudioInputStream(outputAudioFormat, AudioSystem.getAudioInputStream(new File(originalFile) ) );
// The above statement converts the data in the original file to the data filled by the user
} catch( Exception exc ){
System.out.println( exc );
}
String outLoc = null;
JFileChooser saveLoc = new JFileChooser();
int option = saveLoc.showSaveDialog(this);
if( option == JFileChooser.APPROVE_OPTION )
outLoc = saveLoc.getSelectedFile().getAbsolutePath();
try {
if( outputFormat == "AIFF" ) {
AudioSystem.write(newAIS, AudioFileFormat.Type.AIFF, new File(outLoc) );
// the above line gives an error saying that newAis might not have been intialized
} else if( outputFormat == "WAVE") {
AudioSystem.write(newAIS, AudioFileFormat.Type.WAVE, new File(outLoc) );
// the above line gives an error saying that newAis might not have been intialized
} else if( outputFormat == "AU") {
AudioSystem.write(newAIS, AudioFileFormat.Type.AU, new File(outLoc) );
// the above line gives an error saying that newAis might not have been intialized
} else if( outputFormat == "SND") {
AudioSystem.write(newAIS, AudioFileFormat.Type.SND, new File(outLoc) );
// the above line gives an error saying that newAis might not have been intialized
}
} catch( Exception exc ){
}
在上面的代码片段中,我声明了一个 AudioInputStream
类型的变量 newAIS
。 (从开始算起的第 12 条语句) 在下一个语句中,变量 newAIS
被初始化。当我到达 if-else
部分时,变量 newAIS
被认为是 IDE 未初始化的,并且它给出一个错误,指出 newAis 可能尚未初始化 为什么会这样?变量 newAIS 具有函数作用域。
另一方面,如果我将变量 newAIS
声明为全局变量,IDE 不会发现错误。
为什么会发生这种情况?
Following is some function :
jTextField1.setEnabled(false);
jTextField2.setEnabled(false);
jTextField3.setEnabled(false);
jComboBox1.setEnabled(false);
jComboBox2.setEnabled(false);
String samplingRate = jTextField1.getText();
String sampleSize = jTextField2.getText();
String channels = jTextField3.getText();
String endian = (String)jComboBox1.getSelectedItem();
String outputFormat = (String)jComboBox2.getSelectedItem();
AudioFormat outputAudioFormat = new AudioFormat( Float.parseFloat(samplingRate) , Integer.parseInt(sampleSize) , Integer.parseInt(channels) , true , Boolean.parseBoolean(endian) );
AudioInputStream newAIS; // newAIS declared Here
try {
newAIS = AudioSystem.getAudioInputStream(outputAudioFormat, AudioSystem.getAudioInputStream(new File(originalFile) ) );
// The above statement converts the data in the original file to the data filled by the user
} catch( Exception exc ){
System.out.println( exc );
}
String outLoc = null;
JFileChooser saveLoc = new JFileChooser();
int option = saveLoc.showSaveDialog(this);
if( option == JFileChooser.APPROVE_OPTION )
outLoc = saveLoc.getSelectedFile().getAbsolutePath();
try {
if( outputFormat == "AIFF" ) {
AudioSystem.write(newAIS, AudioFileFormat.Type.AIFF, new File(outLoc) );
// the above line gives an error saying that newAis might not have been intialized
} else if( outputFormat == "WAVE") {
AudioSystem.write(newAIS, AudioFileFormat.Type.WAVE, new File(outLoc) );
// the above line gives an error saying that newAis might not have been intialized
} else if( outputFormat == "AU") {
AudioSystem.write(newAIS, AudioFileFormat.Type.AU, new File(outLoc) );
// the above line gives an error saying that newAis might not have been intialized
} else if( outputFormat == "SND") {
AudioSystem.write(newAIS, AudioFileFormat.Type.SND, new File(outLoc) );
// the above line gives an error saying that newAis might not have been intialized
}
} catch( Exception exc ){
}
In the above snippet i declare a variable newAIS
of type AudioInputStream
. (12th statement from starting) In the next statement the variable newAIS
is intialized. When i reach the if-else
part variable newAIS
is said to be uninitialized by the IDE and it gives an error saying newAis might not have been initialized Why is it so ? The variable newAIS
has function scope.
On the other hand if i declare variable newAIS
global , the IDE doesn't spot an error.
Why does this happen ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,它在范围中,但它可能尚未初始化 - 如果发生异常,那么您只需打印出异常并继续,而不是为变量分配值。在这种情况下,您希望
newAIS
具有什么价值?局部变量在读取之前会检查明确的赋值,但实例/静态变量则不会。
请注意,如果您没有继续处理异常,而是向调用者抛出另一个异常,或者只是没有首先捕获它或返回,那么它会没事。目前尚不清楚您真正想要处理的异常是什么 - 一般来说,捕获 Exception 是一种不好的做法。
您可以只为变量分配一个值来开始:
...但是如果分配失败,您真的想继续吗?
另请注意,您当前正在使用
==
比较字符串,这也是一个坏主意。这:大概应该是:
Yes, it's in scope, but it may not have been initialized - if an exception occurs, then instead of the variable being assigned a value, you're just printing out the exception and continuing. What value would you expect
newAIS
to have in that case?Local variables are checked for definite assignment before being read, but instance / static variables aren't.
Note that if you didn't keep going in the fact of an exception, but instead threw another exception up to the caller, or just didn't catch it in the first place, or returned, it would be fine. It's not clear what exceptions you're really trying to handle - catching
Exception
is a bad practice in general.You could just assign the variable a value to start with:
... but do you really want to keep going if the assignment fails?
Also note that you're currently comparing strings using
==
, which is also a bad idea. This:should probably be:
您将变量初始化到 try 块中。如果初始化抛出异常,您只需打印它并继续。在这种情况下你应该抛出异常。
You initialize the variable into try block. If initialization throws exception you just print it and continue. You should throw the exception in this case.
这是因为
newAIS = AudioSystem.getAudioInputStream(...
) 中可能会发生异常,因此 newAIS 永远不会被初始化。您应该执行以下操作:
您可能希望在 catch 中返回,这样就可以避免 <代码>NullPoiterException。
It is because an exception might happen in
newAIS = AudioSystem.getAudioInputStream(...
, so newAIS never gets initialized.You should do the following:
You might want to return in the catch so you could avoid
NullPoiterException
.