1

I'm trying to play sound in Java but it doesn't work and I got error message. Here is my Code

public class PlaySoundClip extends JFrame {


public PlaySoundClip() {
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setTitle("Test Sound Clip");
  this.setSize(300, 200);
  this.setVisible(true);

  try {

     URL url = this.getClass().getClassLoader().getResource("click.wav");
     AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);//Line 27
     // Get a sound clip resource.
     Clip clip = AudioSystem.getClip();
     // Open audio clip and load samples from the audio input stream.
     clip.open(audioIn);
     clip.start();
     clip.loop(Clip.LOOP_CONTINUOUSLY);  // repeat forever
  } catch (UnsupportedAudioFileException e) {
     e.printStackTrace();
  } catch (IOException e) {
     e.printStackTrace();
  } catch (LineUnavailableException e) {
     e.printStackTrace();
  }
}

public static void main(String[] args) {
  new PlaySoundClip();//Line 44
 }
}

And I get this error message then I'm not here any sound! How to fix this problem?

Exception in thread "main" java.lang.NullPointerException   
    at com.sun.media.sound.StandardMidiFileReader.getSequence(StandardMidiFileReader.java:205)   
    at javax.sound.midi.MidiSystem.getSequence(MidiSystem.java:836)   
    at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:174)   
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1145)   
    at playsoundclip.PlaySoundClip.<init>(PlaySoundClip.java:27)   
    at playsoundclip.PlaySoundClip.main(PlaySoundClip.java:44)      

Reference from Playing Sound.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sarin Suriyakoon
  • 420
  • 1
  • 7
  • 19
  • Most probably `url` is `null`. – Konstantin Yovkov Jan 09 '15 at 12:31
  • 1) Is `PlaySoundClip` in a package? 2) Is `click.wav` located in the same path in the Jar? 3) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Jan 09 '15 at 12:32
  • `java.lang.NullPointerException` exception came in `StandardMidiFileReader.java` class provide that class code. – atish shimpi Jan 09 '15 at 12:33
  • Yes, PlaySoundClip is in a package. I try use full path of the file then it still doesn't work. I'm sure that my file path is correct because I've done this resource path with image before. – Sarin Suriyakoon Jan 09 '15 at 15:34
  • *"Yes, PlaySoundClip is in a package."* And what is the package, explicitly? (now is a good time for more information rather than less, since I don't like playing '20 questions'). Your attempt to answer my 2nd question was not very clear. Describe the structure of the directories containing the class and the Wav file. And a tip: Add @kocko (or whoever, the `@` is important) to *notify* a person of a new comment. – Andrew Thompson Jan 10 '15 at 02:26
  • Thank you for your advice,sir. My Directory(Ubuntu) of click.wav is /home/paco/NetBeansProjects/PlaySoundClip/click.wav PlaySoundClip is my project folder in PlaySoundClip it consist of build ,src ,nbproject ,build.xml , manifest.mf. For more information again I try to use full path which I assume that I should work but it was not and I try to move click.wav to any path of the project folder then it still not working. Appriciate all you help @AndrewThompson – Sarin Suriyakoon Jan 10 '15 at 03:56
  • Since you use Netbeans, put `click.wav` inside the `resources` directory. Then reference it as `..getResource("/click.wav");` (note the leading `/`). – Andrew Thompson Jan 10 '15 at 04:14
  • It's not working,sir. I got the same error.@AndrewThompson – Sarin Suriyakoon Jan 10 '15 at 07:10

2 Answers2

1

Something is wrong with click.wav, it was not found on the classpath, so url became null, hence the NullPointerException.

You should put the click.wav on your classpath, so the program will find it. The most straightforward way is putting into the root folder of the jar file.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
1

Most likely in your build phase there is a list of "resources" that are copied from source tree to the output compiled classes. Usually it is a string of regular expressions like ".txt;.json;…" In your case you need to add ".wav" to it or copy file by hand to the compiler output location.

Most IDEs (Eclipse|IteliJ IDEA) and some ant build scripts have provision for resource copying.

Leo
  • 790
  • 8
  • 10
  • Turns out it is a bit more involving in NetBeans but one can do it, see: http://stackoverflow.com/questions/3718201/how-to-add-resources-to-classpath – Leo Jan 10 '15 at 22:39