In Main.java, a file is selected and the absoulute path is returned. This should then be passed to the Logic.java class which will read the top line of that file and return that as a String back to the Main.java class to be displayed in a jTextField. The method within Main.java that should do this reads:
In Logic.java, I am reading the top line and am trying to get it to return to the jTextField. The code for this reads:
However, I get an error message that reads:
unreported exception java.io.IOException; must be caught or declared to be thrown jTextField1.setText(myLogic.readTopLine());
What am I doing wrong. I can't figure out where else I need to put the throws IOException statement.
Many thanks [ April 26, 2007: Message edited by: Sam Bluesman ]
You need a trip to the Java Tutorial here, to read all about Exceptions. This is a simplified inheritance hierarchy for ExceptionsNow the classes I have printed in bold wiht asterisks count as checked Exceptions, and the others (Error and RuntimeException and its subclasses) count as unchecked. This means that the compiler absolutely insists it see error-handling for the checked sort. The commonest checked Exception is IOException.
If you get that error from the compiler, it means you have called something which has "throw new IOException();" somewhere in its code. So you must either throw it ("specify requirement") back up the call stack for another method to deal with, or you must catch and deal with the Exception in the method where the compiler warning occurred ("catch requirement").
You have done one stage of that, specifying the Exception in the readTopLine method; you now have to handle it in whichever method called readTopLine, or change readTopLine to handle the Exception there.
The handling in the calling method may allow you to specify it again and pass the Exception farther up the call stack.
BTW: Two more things in readTopLine: You are getting a String from the readLine() method, then returning toString(). Why? You MUST close the FileReader and BufferedReader when you have finished with them. If you don't know how to do that with a finally block, please start a new thread.