By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,813 Members | 2,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,813 IT Pros & Developers. It's quick & easy.

Error in actionPerformed???

P: n/a
Hey, I'm trying to design a simple interface and I'm getting this
error:

actionPerformed(java.awt.event.ActionEvent) in GUI cannot implement
actionPerformed(java.awt.event.ActionEvent) in
java.awt.event.ActionListener; overridden method does not throw
java.io.IOException
public void actionPerformed(ActionEvent event)

I'm sure this is fairly simple but i'm relatively new to java and
can't work it out!

The code for actionPerformed is as follows, where GetContent and
CompareFiles are outside classes which deal with the URL and GUI is
the name of the overall class...

-----------------------------

public void actionPerformed(ActionEvent event)
throws MalformedURLException, IOException {

try {
URL url = new URL(enterURL.getText());
URLConnection connection = url.openConnection();
GetContent gc = new GetContent();
CompareFiles cf = new CompareFiles();
}

catch (MalformedURLException err)
{
System.err.println("Usage: java URLApp
http://www.URL.com");
}
catch (IOException e)
{
System.err.println("Run software again to check if
content has changed.");
}

-----------------------------

Thanks a lot,
Geoff
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Geoff" <ge******@hotmail.com> wrote in message
news:18**************************@posting.google.c om...
Hey, I'm trying to design a simple interface and I'm getting this
error:

actionPerformed(java.awt.event.ActionEvent) in GUI cannot implement
actionPerformed(java.awt.event.ActionEvent) in
java.awt.event.ActionListener; overridden method does not throw
java.io.IOException
public void actionPerformed(ActionEvent event)

I'm sure this is fairly simple but i'm relatively new to java and
can't work it out!

The code for actionPerformed is as follows, where GetContent and
CompareFiles are outside classes which deal with the URL and GUI is
the name of the overall class...

-----------------------------

public void actionPerformed(ActionEvent event)
throws MalformedURLException, IOException {

*snip code*

The problem is exactly what it says. You can't make a method throw an
exception that doesn't. The actionPerformed method isn't declared to throw
an IOException, therefore you can't try to make it do so. However, at no
time in your code do you throw an IOException anyway. You catch any that
occur, so your method doesn't *need* this declaration, unless you're
planning to change it. I think you need to read up a bit more on Exceptions.
MalformedURLException is a RuntimeException, which is treated a bit
differently and probably won't cause you this problem, but again, you don't
need it there.
Jul 17 '05 #2

P: n/a
ge******@hotmail.com (Geoff) wrote in message news:<18**************************@posting.google. com>...
Hey, I'm trying to design a simple interface and I'm getting this
error:

actionPerformed(java.awt.event.ActionEvent) in GUI cannot implement
actionPerformed(java.awt.event.ActionEvent) in
java.awt.event.ActionListener; overridden method does not throw
java.io.IOException
public void actionPerformed(ActionEvent event)

I'm sure this is fairly simple but i'm relatively new to java and
can't work it out!

The code for actionPerformed is as follows, where GetContent and
CompareFiles are outside classes which deal with the URL and GUI is
the name of the overall class...

-----------------------------

public void actionPerformed(ActionEvent event)
throws MalformedURLException, IOException {

try {
URL url = new URL(enterURL.getText());
URLConnection connection = url.openConnection();
GetContent gc = new GetContent();
CompareFiles cf = new CompareFiles();
}

catch (MalformedURLException err)
{
System.err.println("Usage: java URLApp
http://www.URL.com");
}
catch (IOException e)
{
System.err.println("Run software again to check if
content has changed.");
}

-----------------------------

Thanks a lot,
Geoff

You method does not actually throw those errors. It catches and
handles them. Remove the throws from your method signature.

Greg
Jul 17 '05 #3

P: n/a
Yea I'd fixed the error but wasn't sure how.. but i know now, cheers!
Jul 17 '05 #4

P: n/a
ge******@hotmail.com (Geoff) wrote in message news:<18**************************@posting.google. com>...
Hey, I'm trying to design a simple interface and I'm getting this
error:

actionPerformed(java.awt.event.ActionEvent) in GUI cannot implement
actionPerformed(java.awt.event.ActionEvent) in
java.awt.event.ActionListener; overridden method does not throw
java.io.IOException
public void actionPerformed(ActionEvent event)

I'm sure this is fairly simple but i'm relatively new to java and
can't work it out!


As correctly noted elsewhere, it isn't possible for the exceptions listed
in the 'throws' clause to actually _escape_ from the method, due to the
exception handlers wrapping the main body of the code.

One unrelated, but useful, point: it really isn't good practice to run
lengthy operations - especially network IO where timings are particularly
unpredictable - from within an event handler. Event methods are called
on someone else's thread, so long execution times hog that thread and
prevent it from doing anything else (like processing further events,
perhaps!) Best practice is to exit from an event handler ASAP - firing
off a separate thread of our own if extended processing time is required.
-FISH- ><>
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.