title: Can't play sounds in Java
body: <p>I am trying to add sound effects to my Java game and I am failing to do so. I made the Sound class, I have the .wav files but for some reason I can't hear the sound and I am getting several errors about my code, unknown source or something, I am not sure about that. I will provide you guys with the Sound class, my class calling and the errors.</p>
  
  <p>My folders look like this:
  "...\Java Workspace\SpaceShooter\src" - For my Java .class files.
  "...\Java Workspace\SpaceShooter\src\sounds" - For my java sounds.
  The IDE I'm using is Eclipse.</p>
  
  <p>This is the Sound class</p>
  
  <pre><code>import java.applet.Applet;
  import java.applet.AudioClip;
  
  public class Sound {
      public static final Sound laser = new Sound("sounds/laser.wav");
      private AudioClip clip;
  
      public Sound (String filename){
          try{
              clip = Applet.newAudioClip(Sound.class.getResource(filename));
          }catch(Exception e){
              e.printStackTrace();
          }
      }
  
      public void play(){
          try{
              new Thread(){
                  public void run(){
                      clip.play();
                  }
              }.start();
          }catch(Exception ex){
              ex.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>This is how I call my Sound class.</p>
  
  <pre><code>...
  if (code == KeyEvent.VK_UP) 
      if (!player1weapon.isFalling){
           player1weapon.isFalling = true;
           Sound.laser.play();
      }
  </code></pre>
  
  <hr>
  
  <p>And the errors</p>
  
  <pre><code>java.lang.NullPointerException
      at sun.applet.AppletAudioClip.&lt;init&gt;(Unknown Source)
      at java.applet.Applet.newAudioClip(Unknown Source)
      at Sound.&lt;init&gt;(Sound.java:10)
      at Sound.&lt;clinit&gt;(Sound.java:5)
      at SpaceInvadersPanel$4.keyPressed(SpaceInvadersPanel.java:71)
      at java.awt.Component.processKeyEvent(Unknown Source)
      at javax.swing.JComponent.processKeyEvent(Unknown Source)
      at java.awt.Component.processEvent(Unknown Source)
      at java.awt.Container.processEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
      at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
      at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
      at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
      at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Window.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
      at java.awt.EventQueue.access$200(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.awt.EventQueue$4.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
  Exception in thread "Thread-2" java.lang.NullPointerException
      at Sound$1.run(Sound.java:20)
  </code></pre>