Error in actionPerformed???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff

    Error in actionPerformed???

    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.IOExcep tion
    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 MalformedURLExc eption, IOException {

    try {
    URL url = new URL(enterURL.ge tText());
    URLConnection connection = url.openConnect ion();
    GetContent gc = new GetContent();
    CompareFiles cf = new CompareFiles();
    }

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

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

    Thanks a lot,
    Geoff
  • Ryan Stewart

    #2
    Re: Error in actionPerformed ???

    "Geoff" <geoff104@hotma il.com> wrote in message
    news:18143a5b.0 403030230.3762e [email protected] gle.com...[color=blue]
    > 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.IOExcep tion
    > 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 MalformedURLExc eption, IOException {[/color]
    *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.
    MalformedURLExc eption is a RuntimeExceptio n, which is treated a bit
    differently and probably won't cause you this problem, but again, you don't
    need it there.


    Comment

    • Gregory A. Swarthout

      #3
      Re: Error in actionPerformed ???

      geoff104@hotmai l.com (Geoff) wrote in message news:<18143a5b. 0403030230.3762 [email protected] ogle.com>...[color=blue]
      > 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.IOExcep tion
      > 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 MalformedURLExc eption, IOException {
      >
      > try {
      > URL url = new URL(enterURL.ge tText());
      > URLConnection connection = url.openConnect ion();
      > GetContent gc = new GetContent();
      > CompareFiles cf = new CompareFiles();
      > }
      >
      > catch (MalformedURLEx ception err)
      > {
      > System.err.prin tln("Usage: java URLApp
      > http://www.URL.com");
      > }
      > catch (IOException e)
      > {
      > System.err.prin tln("Run software again to check if
      > content has changed.");
      > }
      >
      > -----------------------------
      >
      > Thanks a lot,
      > Geoff[/color]


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

      Greg

      Comment

      • Geoff

        #4
        Re: Error in actionPerformed ???

        Yea I'd fixed the error but wasn't sure how.. but i know now, cheers!

        Comment

        • FISH

          #5
          Re: Error in actionPerformed ???

          geoff104@hotmai l.com (Geoff) wrote in message news:<18143a5b. 0403030230.3762 [email protected] ogle.com>...[color=blue]
          > 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.IOExcep tion
          > 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![/color]

          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- ><>

          Comment

          Working...