为什么我得到一个创建文件的空指针异常?

时间:2016-03-26 16:10:55

标签: java nullpointerexception

我在类中创建一个文件,我想传递文件路径以在另一个类中使用该文件。当我尝试在另一个类中使用该文件时,我收到以下错误:

有谁知道为什么它看不到文件? 字符串saveFilePath初始化在Mainframe类的顶部。如果我硬编码路径它工作正常,但当我作为字符串saveFilePath传递路径时,我得到错误。感谢

   private void saveFile() {
    JFileChooser fileChooser = new JFileChooser();
    FileFilter wavFilter = new FileFilter() {
        @Override
        public String getDescription() {
            return "Sound file (*.WAV)";
        }

        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else {
                return file.getName().toLowerCase().endsWith(".wav");
            }
        }
    };

    fileChooser.setFileFilter(wavFilter);
    fileChooser.setAcceptAllFileFilterUsed(false);

    int userChoice = fileChooser.showSaveDialog(this);
    if (userChoice == JFileChooser.APPROVE_OPTION) {
        saveFilePath = fileChooser.getSelectedFile().getAbsolutePath();
        if (!saveFilePath.toLowerCase().endsWith(".wav")) {
            saveFilePath += ".wav";
        }

        File wavFile = new File(saveFilePath);

        try {
            recorder.save(wavFile);
            buttonPlay.setEnabled(true);
            Keyup.setEnabled(true);
            Keydown.setEnabled(true);
            btnSave.setEnabled(true);
            getKey.setEnabled(true);


        } catch (IOException ex) {
            JOptionPane.showMessageDialog(Mainframe.this, "Error",
                    "Error saving to sound file!",
                    JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
    }
}


public class notehandle extends Mainframe{


String s = saveFilePath;
File f = new File(s);
float pitchInHz;

public float note() {

    PitchDetectionHandler pdh = new PitchDetectionHandler() {

        public void handlePitch(PitchDetectionResult result,AudioEvent e) {

           pitchInHz = result.getPitch();



        }



    };

    AudioDispatcher dispatcher = null;
    try {
        dispatcher = AudioDispatcherFactory.fromFile(f, 1024, 0);
    } catch (UnsupportedAudioFileException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    AudioProcessor p = new PitchProcessor(PitchEstimationAlgorithm.FFT_YIN, 48000, 1024, pdh);
    dispatcher.addAudioProcessor(p);
    new Thread(dispatcher,"Audio Dispatcher").start();


    return pitchInHz;



}

}

0 个答案:

没有答案