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 ?
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
newAISto 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
Exceptionis 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: