title: How to use KeyListener with audio?
body: <p>Well, I'm trying to make a program simulating a launchpad. But for that, I need to open the program and press, for example, the "Q" key, it play a sound. How could I do this? In short: "Handshake specifies a key, and while it is being pressed, tap a sound" If you can answer, even grateful! xD
  I click a button on my keyboard, and an error, the following code appears music: </p>
  
  <pre><code>try {
  AudioInputStream sound = AudioSystem.getAudioInputStream (getClass () getResource       ("1.wav").); 
  DataLine.Info info = new DataLine.Info (Clip.class, sound.getFormat ()); 
  Clip clip = (Clip) AudioSystem.getLine (info); 
  clip.open (sound); 
  clip.start (); 
  if (playing == false) {
  clip.stop (); 
  Else {} 
  clip.start (); 
  } 
  } Catch (Exception e) {
  JOptionPane.showMessageDialog (null, "Fatal err"); 
  } 
  </code></pre>
  
  <p>I created a KeyListener so that when I click, the sound plays, but the following error appears: </p>
  
  <pre><code>java.lang.NullPointerException 
  com.sun.media.sound.StandardMidiFileReader.getSequence at (Unknown Source) 
  javax.sound.midi.MidiSystem.getSequence at (Unknown Source) 
  com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream at (Unknown Source) 
  javax.sound.sampled.AudioSystem.getAudioInputStream at (Unknown Source) 
  at Programa.LaunchPad.som1 (LaunchPad.java: 41) 
  Programa.LaunchPad at $ BotãoAção.keyReleased (LaunchPad.java: 201) 
  java.awt.Component.processKeyEvent at (Unknown Source) 
  at java.awt.Component.processEvent (Unknown Source) 
  at java.awt.Container.processEvent (Unknown Source) 
  java.awt.Window.processEvent at (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) 
  java.awt.KeyboardFocusManager.redispatchEvent at (Unknown Source) 
  java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent at (Unknown Source) 
  java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent at (Unknown Source) 
  java.awt.DefaultKeyboardFocusManager.typeAheadAssertions at (Unknown Source) 
  java.awt.DefaultKeyboardFocusManager.dispatchEvent at (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) 
  java.awt.EventQueue.dispatchEventImpl at (Unknown Source) 
  java.awt.EventQueue.access at $ 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) 
  java.security.ProtectionDomain at $ 1.doIntersectionPrivilege (Unknown Source) 
  java.security.ProtectionDomain at $ 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) 
  java.security.ProtectionDomain at $ 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)
  </code></pre>
  
  <p>Im using the library AWT, and Swing. (With JPanel, ImageIcon, sound)</p>