java - NullPointerException w/ Sound Class -
i building game engine , inside of engine there class, called sound.java. purpose of class load sound effects engine , play them. problem inside constructor of sound.java, it's throwing exception called, nullpointerexception; however, doesn't make sense referencing correct file path of said sound file.
take @ below code + stack trace , please me figure out!
sound.java:
public class sound { private clip clip; public sound(string filepath) { try { system.out.println(filepath); audioinputstream ais = audiosystem.getaudioinputstream(getclass().getresourceasstream(filepath)); audioformat baseformat = ais.getformat(); audioformat decodeformat = new audioformat(audioformat.encoding.pcm_signed, baseformat.getsamplerate(), 16, baseformat.getchannels(), baseformat.getchannels() * 2, baseformat.getsamplerate(), false); audioinputstream dais = audiosystem.getaudioinputstream(decodeformat, ais); clip = audiosystem.getclip(); clip.open(dais); } catch(exception e) { e.printstacktrace(); cedebug.showdebugmessage("sound file not found"); } }
cesound.java:
public class cesound { private static map<string, sound> soundmap = new hashmap<string, sound>(); //sound effects hashmap /** * adds new sound hash map<br> * file must <strong>.wav</strong> format * @param key key or id used access sound effect * @param filename name of sound file */ public static void addsound(string key, string filename) { try { soundmap.put(key, new sound(referencelibrary.soundlocation + filename)); //error: (3/4/15) sound file not found! } catch (exception e) { e.printstacktrace(); cedebug.showdebugmessage("sound file not found"); } }
referencelibrary.java:
public class referencelibrary { public static final string resourcelocation = "./resources/"; public static final string soundlocation = resourcelocation + "audio/sound/"; }
stack trace:
java.lang.nullpointerexception @ com.sun.media.sound.softmidiaudiofilereader.getaudioinputstream(softmidiaudiofilereader.java:130) @ javax.sound.sampled.audiosystem.getaudioinputstream(audiosystem.java:1113) @ coreengine.resourcelibrary.other.sound.<init>(sound.java:16) @ coreengine.resourcelibrary.cesound.addsound(cesound.java:21) @ resourcelibrary.resourceloader.loadsounds(resourceloader.java:48) @ gamestates.menustate.init(menustate.java:25) @ gamestates.menustate.<init>(menustate.java:21) @ gamestates.gamestatemanager.loadstate(gamestatemanager.java:27) @ gamestates.gamestatemanager.<init>(gamestatemanager.java:22) @ gameengine.core.init(core.java:29) @ coreengine.gameengine.coreengine.<init>(coreengine.java:55) @ gameengine.core.<init>(core.java:24) @ gameengine.core.main(core.java:62)
thanks!
-nick
(1) consider using tinysound (on github). free, easy use, pretty small, basic library.
(2) i'd recommend way (from javax.sound.sampled) sound data: audiosystem.getaudioinputstream(url)
or used form
audiosystem.getaudioinputstream(file)
to this, first define url or file. added benefit, easier debug/verify whether have made contact file prior creating audioinputstream.
Comments
Post a Comment