Null Pointer Exception in Java audio player

  Kiến thức lập trình

took some coding classes many years ago and want to throw together a simple audioplayer app. Went to chatGPT for help as I don’t really remember anything properly. The generated code doesn’t throw any errors in eclipse until I run it then I get a null pointer exception on line 15. Tried youtube troubleshooting but can’t see how this is a null pointer or how to instantiate it properly so here I am asking your help.

This is a short code as I wanted to start simple with just getting it to play an audio file at all then build out from there.

Line 15 is the unindented starting AdioSystem.getAudioInputStream(…);

package dryFyr;
import javax.sound.sampled.*;

public class dryfyr {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //chatGPT insert!
        try {
            // Specify the path to your audio file
            String audioFilePath = "C:/Users/diazd/Desktop/dryfyr/standing.wav";

            // Open the audio input stream
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(dryfyr.class.getResource(audioFilePath));

            // Get a clip resource
            Clip clip = AudioSystem.getClip();

            // Open audio clip and load samples from the audio input stream
            clip.open(audioStream);

            // Start playing the audio clip
            clip.start();

            // Wait for the clip to finish playing
            while (!clip.isRunning())
                Thread.sleep(10);
            while (clip.isRunning())
                Thread.sleep(10);

            // Close resources
            clip.close();
            audioStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        //end chatGPT insert

    }

}

Again I just tried to copy past chatGPT code, and I expected it to just play the audio file specified in the file path.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT