try catch exception -_- I'm at my WITS END!

ok, so I have this problem with this bit of code. I'm trying to make a pong game (I'm relatively new to java, so be nice!), and the roadblock I come to is when I try and run this bit of code to get a SOUND to play when it bounces off a paddle:

public void init()

{

try {

URL soundFile2URL = new URL("http://irc.zockwelten.de/pjirc/snd/ding.au");

}

catch(MalformedURLException SoundFile2URL) {}

if (soundFile2URL != null)

{

soundFile2 = getAudioClip(soundFile2URL2);

soundFile2.play();

}

}

the init() proc is called at the beginning of my main program, and I keep getting the error "NullpointerException" "null (in.java.Applet)

the main program is an applet, and it compiles just fine.....the only problem is when it actually tries to run the code. asldkfjskld

HELP!

[865 byte] By [B34ST1Ya] at [2007-11-26 19:51:23]
# 1

This line:

URL soundFile2URL = new URL("http://irc.zockwelten.de/pjirc/snd/ding.au");

should be:

soundFile2URL = new URL("http://irc.zockwelten.de/pjirc/snd/ding.au");

because you want to assign to a field, not just a local variable.

Later in your code, you mention first soundFile2URL then soundFile2URL2.

Is that a typo or the bug?

DrLaszloJamfa at 2007-7-9 22:41:22 > top of Java-index,Java Essentials,Java Programming...
# 2
oh. I think that's a typo. OOPS.
B34ST1Ya at 2007-7-9 22:41:22 > top of Java-index,Java Essentials,Java Programming...
# 3
prob solv?
DrLaszloJamfa at 2007-7-9 22:41:23 > top of Java-index,Java Essentials,Java Programming...
# 4
I changed that URL thing, and I still get the same error. It doesn't crash out when I declare the file location, only when I try to assign my "soundFile2" variable to the "SoundFile2URL" variable. It's like it's not even writing the location to the URL variable.... :(
B34ST1Ya at 2007-7-9 22:41:23 > top of Java-index,Java Essentials,Java Programming...
# 5
Post a tiny (< 1 page) complete sample program that demonstrates your problem.
DrLaszloJamfa at 2007-7-9 22:41:23 > top of Java-index,Java Essentials,Java Programming...
# 6

here i'll do this. the critical program code is essentially just a procedure at the top of the program, that is called upon later in the program....so i'll paste up TO the end of the procedure.

--

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.Component;

import java.awt.Button;

import java.net.* ;

import java.applet.*;

public class Move extends Applet

{

JFrame f1;

JPanel p1,p2,p3;

JButton left,right,stop,b1;

GraphicsPanel g1,g2;

int aix, x1;

int dir = 0;

int dir2 = 0;

int bdirx = 2;

int bdiry = 2;

int ballx = 100;

int bally = 100;

int speed =1 ;

AudioClip soundFile;

URL soundFileURL;

public void init()

{

try {

soundFileURL = new URL("http://irc.zockwelten.de/pjirc/snd/ding.au");

}

catch (MalformedURLException SoundFileURL) {}

soundFile = getAudioClip(soundFileURL);

}

public Move()

{

init();

-

that last public Move() is the beginning of the main program, and as soon as init() [the procedure for the sound clip] is called...it crashes.

B34ST1Ya at 2007-7-9 22:41:23 > top of Java-index,Java Essentials,Java Programming...
# 7

Hmmmm... I never touch applets, but I believe you should

1. Avoid doing anything tricky in the applet's constructor because the applet context may not be properly set up at that point.

2. Your init() method will be called by the framework. There is no need to

explicitly call it.

So my advice is to remove your constructor.

3. Your init method is a bit dodgy. You should never stiffle exceptions. At

the very least, print the stack trace. And if your URL is malformed, you

call get audio clip anyway!

DrLaszloJamfa at 2007-7-9 22:41:23 > top of Java-index,Java Essentials,Java Programming...
# 8

well the only reason I converted the application into an applet.....is because 90% of the tutorials online said it was easier in an applet...and I had already (being the newbie I am) constructed my program in a non-applet setup.

Is there a way to get the audio clip to play, NOT using an applet setup?

B34ST1Ya at 2007-7-9 22:41:23 > top of Java-index,Java Essentials,Java Programming...
# 9

I always say that, if you are learning Swing, you will have far fewer headaches

writing standalone apps with JFrames than you will have will JApplets.

You seem to be stuck in the old AWT -- I would suggest moving up to Swing.

Anyhowdy, the one exception to my saying may be audio, because (j)applets

make it easy to play audio clips. I can't really say more, because I've never done any

sound (I like my programs totally silent), but have you

studied the sound tutorial: http://java.sun.com/docs/books/tutorial/sound/index.html

DrLaszloJamfa at 2007-7-9 22:41:23 > top of Java-index,Java Essentials,Java Programming...
# 10
where can I add extra packages? It says the class I typed in doesn't exist (in the codebas, obviously)
B34ST1Ya at 2007-7-9 22:41:23 > top of Java-index,Java Essentials,Java Programming...
# 11

> where can I add extra packages? It says the class I

> typed in doesn't exist (in the codebas, obviously)

Are you still trying to write an applet? Perhaps your tag is wrong.

Are you jarring your classes. Take a look at tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html

Perhaps someone else can come forward. As I wrote, I avoid applets.

DrLaszloJamfa at 2007-7-9 22:41:23 > top of Java-index,Java Essentials,Java Programming...
# 12
no all I want is a successful way to play a 1/2 second audio clip when the ball bounces off the paddle..........I have absolutely NO concern as to how I get there. ;) haha
B34ST1Ya at 2007-7-9 22:41:24 > top of Java-index,Java Essentials,Java Programming...
# 13
Try that sound tutorial then.
DrLaszloJamfa at 2007-7-9 22:41:24 > top of Java-index,Java Essentials,Java Programming...
# 14
ugh...I still can't get it.can someone PLEASE just past...like the ABSOLUTE most barebones *complete* code for playing ONE sound? no buttons....no anything....just ONE time playing the sound.
B34ST1Ya at 2007-7-9 22:41:24 > top of Java-index,Java Essentials,Java Programming...
# 15
Have you tried the "Java Sound" forum: http://forum.java.sun.com/forum.jspa?forumID=541I took a look and the first thread I saw was this: http://forum.java.sun.com/thread.jspa?threadID=793220
DrLaszloJamfa at 2007-7-21 17:44:47 > top of Java-index,Java Essentials,Java Programming...
# 16
anyone? please?
B34ST1Ya at 2007-7-21 17:44:47 > top of Java-index,Java Essentials,Java Programming...
# 17
that's what I'm asking. Can someone *please* just humor me, and write up an allegedly "simple" code to get a sound clip to play ONCE.....as simple as possible.
B34ST1Ya at 2007-7-21 17:44:47 > top of Java-index,Java Essentials,Java Programming...
# 18
update. I *did* get it. thanks guys. : )
B34ST1Ya at 2007-7-21 17:44:47 > top of Java-index,Java Essentials,Java Programming...