Skip to main content
gamedev.net
🔒 Locked

[java] What is wrong????

Started by CaedmonSoftware Apr 16, 2008 at 6:30 PM 0 replies 650+ views
Original Post
CaedmonSoftware
CaedmonSoftware
Hello, I wrote an game using java applet technologies and am trying to integrate sound into the game. The code that loads the song is throwing an malformed // Use the absolute URL of the sound file URL oursong = new URL; oursong = "http://www.servername.net/media/sound.mp3"; AudioClip gamesound = getAudioClip(oursong); gamesound.play(); *** this code when compiled gives me the error: /tmp/4230/spaceinvader.java:186: '(' or '[' expected URL oursong = new URL; ^ now, i thought maybe i can try this and it will work... URL oursong = new URL("http://www.servername.net/media/sound.mp3"); however it does not work and gives me an different error: /tmp/8154/spaceinvader.java:186: unreported exception java.net.MalformedURLException; must be caught or declared to be thrown URL oursong = new URL("songname.mp3"); So, what am i doing wrong? Thanks
sphen_lee
sphen_lee
In the first example you need to place () after new URL, that is assuming URL has a constructor with no arguments.
In the second example you need to place the new URL(...) and code that depends on it into a try block, because it may throw an exception, and Java forces you to catch all exceptions that it has been told about. (Alternatively you can tell Java that you will be throwing it, look up the 'throws' clause)

Like this:

try{    URL ourUrl = new URL("...");    //do stuff here with ourUrl, load the music for example}catch(MalformedURLException e){    // Do something about the URL being malformed    // probably a fatal error, since the url is hard coded}//possibly catch other exceptions here, the compiler will tell you :P

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.