mystified with javax.sound.sampled.Clip NullPointerException -


mystified javax.sound.sampled.clip nullpointerexception running on eclipse on mac. input wave file exists, constructor works fine. object instance created. can't access instance methods, of them. java 101 issue here, apologize in advance, if so? or eclipses 101, matter...

public class audiocliptester {  public static void main(string[] args)  {     // todo auto-generated method stub      audioclipplayer moomoo = new audioclipplayer("cow.wav");     moomoo.play(); } } 

/=====

import java.io.file; import java.io.ioexception; import java.net.malformedurlexception; import javax.sound.sampled.audioinputstream; import javax.sound.sampled.audiosystem; import javax.sound.sampled.clip; import javax.sound.sampled.lineunavailableexception; import javax.sound.sampled.unsupportedaudiofileexception;  /**  * handles play, pause, , looping of sounds game.  * @author tyler thomas  *  */ public class audioclipplayer  {     private clip myclip;     public audioclipplayer(string filename) {             try {                 file file = new file(filename);                 if (file.exists()) {                     clip myclip = audiosystem.getclip();                     system.out.println("file "+filename+" in root dir");                     audioinputstream ais = audiosystem.getaudioinputstream(file.touri().tourl());                     myclip.open(ais);                     system.out.println("ais "+ais.tostring()+" open");                     }                 else {                     throw new runtimeexception("sound: file not found: " + filename);                 }             }             catch (malformedurlexception e) {                 throw new runtimeexception("sound: malformed url: " + e);             }             catch (unsupportedaudiofileexception e) {                 throw new runtimeexception("sound: unsupported audio file: " + e);             }             catch (ioexception e) {                 throw new runtimeexception("sound: input/output error: " + e);             }             catch (lineunavailableexception e) {                 throw new runtimeexception("sound: line unavailable: " + e);             }     }     public void play(){         system.out.println("clip "+myclip.tostring()+" play");         myclip.setframeposition(0);  // must rewind!         myclip.loop(0);         myclip.start();  //           thread.sleep(10000);        }     public void loop(){         myclip.loop(clip.loop_continuously);     }     public void stop(){         myclip.stop();     } 

}

that because in following line:

 clip myclip = audiosystem.getclip(); 

you declare , initialize local variable , field myclip stays null. try replace above line with

 myclip = audiosystem.getclip(); 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -