Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with IOException

Status
Not open for further replies.

wcj00

Programmer
Aug 12, 2003
10
US
I have written an application that has a GUI that accepts a file name in a text area box. When the user clicks the submit button the application is suppose to read in the file name the user entered and return the length of the longest line. In this application I have a method that reads in the file and finds the length of the longest line which throws a IO exception. I want to call this method from my event handler but upon compilation I receive an error.---> action (java.awt.event,java.lang.object)in findlength can not override action (java.awt.event,java.lang.object)in java.awt.component; overriden method does not throw java.io.IOException
public boolean action(event evt, object arg)throws IOException
 
Looks like your method which you are overriding does not throw IOExcpetion ...

Change : action(event evt, object arg)
To : action(event evt, object arg) throws IOException
 
sedj,
I have the "throws IOException" in there. When I don't have it in there I receive a different error -->
unreported exception java.io.IOException; must be caught or declared to be thrown doit()
^
 
The problem is that the function
Code:
action
that your overriding doesn't throw an exception, so you can't make the function you're writing change that (i.e. you can't just add "
Code:
throws IOException
" to your version of action)

to fix this you're going to have to catch the exception in the function that's opening the file - assuming your current doit function looks something like
Code:
public void doit(){
 FileInputStream fis = new FileInputStream(...);
 ...
}
[code]

put a [code]try/catch
around your code - i.e. change it to
Code:
public void doit(){
try{
 FileInputStream fis = new FileInputStream(...);
 ...
}catch(IOException ioex){
 ioex.printStackTrace();
}
}

Hope this helps!
 
If I read your first post correctly, it states :

; overriden method does not throw java.io.IOException

and not

; overriding method does not throw java.io.IOException

So I think you should keep
Code:
action(event evt, object arg)

but in the method, catch the exception, handle it there and do not throw it further.

By the way :

action(event evt, object arg)
Deprecated. As of JDK version 1.1, should register this component as ActionListener on component which fires action events.
 
I do have the try statement in my method and handle the exception within the method. I posted my code below.

import java.lang.Character;
import java.awt.*;
import java.io.*;
public class findlength extends Frame{

TextField filename=new TextField(20);
TextField longestline= new TextField(5);
TextField linenumber= new TextField(7);
String name;

Label reccount=new Label("THE LONGEST LINE IS ");
Label linenum=new Label("FOUND AT LINE NUMBER ");


public void doit() throws IOException
{

String file="init";
String line="init";
int largestrownumber=0;
int largestrecordlength=0;
int i=0;
File o =new File(name);

try{ BufferedReader reader1=new BufferedReader(new FileReader(o));

line=reader1.readLine();
while(line!=null)
{
i++;
if(line.length()>largestrecordlength)
{largestrecordlength=line.length();
largestrownumber=i;}
line=reader1.readLine();
}

//longestline.setText(largestrecordlength);
//linenumber.setText(largestrownumber);
// convert int to string and display.....

} catch (IOException e) {e.printStackTrace();}//end of try function

}//end of function doit

public findlength(){
super("FIND LENGTH APPLICATION");
setLayout(new BorderLayout());
initializebuttonbox();
initializecenter();
pack();
resize(300,300);
show();
}


public void initializebuttonbox()
{
Panel b=new Panel();
b.setLayout(new FlowLayout());
Button submit=new Button("SUBMIT");
b.add("South",submit);
Button reset=new Button("RESET");
b.add("South",reset);
Button close=new Button("CLOSE");
b.add("South",close);
add("South",b);
}

public void initializecenter()
{
Panel c=new Panel();
c.setLayout(new GridLayout(6,1));
c.add(new Label(" PLEASE ENTER THE FILE NAME BELOW "));
c.add(filename);
c.add(reccount);
c.add(longestline);
c.add(linenum);
c.add(linenumber);


add("Center",c);
}

public boolean action(Event evt, Object arg) throws IOException
{
if(arg.equals("CLOSE"))
{
dispose();
return true;
}

if(arg.equals("RESET"))
{
filename.setText("");
longestline.setText("");
linenumber.setText("");
return true;
}
if(arg.equals("SUBMIT"))
{
name=filename.getText();
doit();
return true;

}

return super.handleEvent(evt); }//end of event handler




public static void main(String args[])
{new findlength();

}//end of main function


}//end of class
 
I do have the try statement in my method and handle the exception within the method. I posted pieces of my code below.

public void doit() throws IOException
{

String file="init";
String line="init";
int largestrownumber=0;
int largestrecordlength=0;
int i=0;
File o =new File(name);

try{ BufferedReader reader1=new BufferedReader(new FileReader(o));

line=reader1.readLine();
while(line!=null)
{
i++;
if(line.length()>largestrecordlength)
{largestrecordlength=line.length();
largestrownumber=i;}
line=reader1.readLine();
}


// convert int's to string and display.....



} catch (IOException e) {e.printStackTrace();}//end of try function

}//end of function doit


public boolean action(Event evt, Object arg) throws IOException
{
if(arg.equals("CLOSE"))
{
dispose();
return true;
}

if(arg.equals("RESET"))
{
filename.setText("");
longestline.setText("");
linenumber.setText("");
return true;
}
if(arg.equals("SUBMIT"))
{
name=filename.getText();
doit();
return true;

}

return super.handleEvent(evt); }//end of event handler
 
It is the action method that should NOT throw the IOException and which therefore should contain the try/catch code...
 
Hologram,
Thank you very much, that worked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top