-1

I'm having a simple program that plays a sound when running it.

code:

public static void main(String[] args) {
     try {
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(Main.class.getResource("ProgramAudio\\chechen.wav"));
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            clip.start();
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
}

Error:

java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:222)
at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1032)
at aChatProgramAudio.audio2.main(audio2.java:13)

Maybe the given directory has something to do with it but it does exists.

syclone
  • 99
  • 13
  • `Main.class.getResource("ProgramAudio\\chechen.wav")` is returning `null`. Is `ProgramAudio` directory in the [classpath](https://stackoverflow.com/questions/2396493/what-is-a-classpath-and-how-do-i-set-it)? It needs to be if you want to load it as a resource. – Kayaman Sep 01 '20 at 16:33
  • The wav file is inside the same folder of the program – syclone Sep 01 '20 at 16:37
  • "Is ProgramAudio directory in the classpath?" – Kayaman – syclone Sep 01 '20 at 16:50
  • But why did you change your comment tho? – syclone Sep 01 '20 at 16:54
  • Because it needed to be changed and comments are editable for five minutes. – Kayaman Sep 01 '20 at 17:05
  • Thank you for the link. I didn't notice it at first ;) – syclone Sep 01 '20 at 17:08
  • 1
    The argument to `getResource` must be a relative URL. URLs *always* use forward slashes (`/`), on all platforms. – VGR Sep 01 '20 at 18:32
  • If you are still having problems, sharing the location of the audio resource and the structure of your source code folder (especially the location of Main.java) would be helpful. – Phil Freihofner Sep 01 '20 at 23:53
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Sep 20 '20 at 15:34

1 Answers1

1

Some possibilities to try.

In this one, have chechen.wav in the same package folder as Main.java.

URL url = Main.class.getResource("chechen.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);

If your package folder structure is something like the following (where ProgramAudio is a package under src):

src/yourProgramCodePackage/Main.java
src/ProgramAudio/chechen.wav

Then the following should work, using "absolute" addressing.

URL url = Main.class.getResource("/ProgramAudio/chechen.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);

If you have the following folder structure, where ProgramAudio is a subfolder of the code package with Main.java in it, then the "relative" form of addressing should work.

src/yourProgramCodePackage/Main.java
src/yourProgramCodePackage/ProgramAudio/chechen.wav

URL url = Main.class.getResource("ProgramAudio/chechen.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);

Probably a good idea to follow convention for package names and stick with lower or camel case, e.g., programAudio if that is being used as a package name.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41