当变量被声明为具有函数作用域时,我收到类型错误

发布于 2024-12-03 20:44:14 字数 2749 浏览 1 评论 0原文

以下是一些函数:

        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 技术交流群。

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

发布评论

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

评论(3

荭秂 2024-12-10 20:44:14

是的,它在范围中,但它可能尚未初始化 - 如果发生异常,那么您只需打印出异常并继续,而不是为变量分配值。在这种情况下,您希望 newAIS 具有什么价值?

局部变量在读取之前会检查明确的赋值,但实例/静态变量则不会。

请注意,如果您没有继续处理异常,而是向调用者抛出另一个异常,或者只是没有首先捕获它或返回,那么它会没事。目前尚不清楚您真正想要处理的异常是什么 - 一般来说,捕获 Exception 是一种不好的做法。

可以只为变量分配一个值来开始:

AudioInputStream newAIS = null;

...但是如果分配失败,您真的想继续吗?

另请注意,您当前正在使用 == 比较字符串,这也是一个坏主意。这:

if (outputFormat == "AIFF")

大概应该是:

if (outputFormat.equals("AIFF"))

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:

AudioInputStream newAIS = null;

... 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:

if (outputFormat == "AIFF")

should probably be:

if (outputFormat.equals("AIFF"))
⒈起吃苦の倖褔 2024-12-10 20:44:14

您将变量初始化到 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.

对不⑦ 2024-12-10 20:44:14

这是因为 newAIS = AudioSystem.getAudioInputStream(...) 中可能会发生异常,因此 newAIS 永远不会被初始化。

您应该执行以下操作:

AudioInputStream newAIS = null;  // newAIS declared Here 
try {
   newAIS = AudioSystem.getAudioInputStream(outputAudioFormat, AudioSystem.getAudioInputStream(new File(originalFile) ) );

   } catch( Exception exc ){
         // handle the exception properly not just write something out!
}

您可能希望在 catch 中返回,这样就可以避免 <代码>NullPoiterException。

It is because an exception might happen in newAIS = AudioSystem.getAudioInputStream(..., so newAIS never gets initialized.

You should do the following:

AudioInputStream newAIS = null;  // newAIS declared Here 
try {
   newAIS = AudioSystem.getAudioInputStream(outputAudioFormat, AudioSystem.getAudioInputStream(new File(originalFile) ) );

   } catch( Exception exc ){
         // handle the exception properly not just write something out!
}

You might want to return in the catch so you could avoid NullPoiterException.

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