0

So I am creating an application that plays audio-files in the background using clips() and I added a JSlider that allows the user to alter the volume of the audio-file. However, whenever I click the JSlider it gives me the following errors:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GoRacer.stateChanged(GoRacer.java:764)
at javax.swing.JSlider.fireStateChanged(Unknown Source)
at javax.swing.JSlider$ModelListener.stateChanged(Unknown Source)
at javax.swing.DefaultBoundedRangeModel.fireStateChanged(Unknown Source)
at javax.swing.DefaultBoundedRangeModel.setRangeProperties(Unknown Source)
at javax.swing.DefaultBoundedRangeModel.setValueIsAdjusting(Unknown Source)
at javax.swing.JSlider.setValueIsAdjusting(Unknown Source)
at javax.swing.plaf.basic.BasicSliderUI$TrackListener.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)

My code for my audio-player method is:

  public void musicPlayer (String filePath, String loopYesNo, float userInput, long position, Clip a){


try{
  File fileSong = new File (filePath); 
  AudioInputStream input = AudioSystem.getAudioInputStream(fileSong); 
  a = AudioSystem.getClip();
  a.open(input); 
  a.setMicrosecondPosition(position); 

  FloatControl gainControl = (FloatControl) a.getControl(FloatControl.Type.MASTER_GAIN); 
  float range = (float) (Math.log(userInput) / Math.log(10) * 20); 
  gainControl.setValue(range); 
  a.start(); 


  if (loopYesNo.equals("Yes")) {
    a.loop(Clip.LOOP_CONTINUOUSLY);
  }

} catch (Exception error) {
  errorEnd(); 
}

}

And the code for the JSlider is:

  public void stateChanged (ChangeEvent e) {


if (e.getSource() == volume) {

  long clipTimePosition = clipMusic.getMicrosecondPosition(); 
  clipMusic.stop(); 


  programVolume =  (float)volume.getValue() / 100;  


  musicPlayer(mainFrameMusic, "Yes", programVolume, clipTimePosition, clipMusic);
}

}

If I get rid of Clip in input parameters it seems to work fine;however, seeing that I will be needing to listen to multiple audio-files in this program it would be nice to use one method for all of them.

0 Answers0