Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

cannot find symbol

Thread view: 
fybar - 25 Apr 2006 18:52 GMT
New to java.  I have a snippet of code that I can compile fine on a FreeBSD
box running 1.1.4, or something similar:

    FileInputStream fs = new FileInputStream(f);
    InputStreamReader sirLine;
    BufferedReader fileInput;
    sirLine = new InputStreamReader(fs);
    fileInput = new BufferedReader(sirLine);
    while ( j <= 53 ) {
        fileline = fileInput.readLine();

I just installed NetBeans 5.0 on WinXP as my FreeBSD box isn't portable.  I
loaded the source files and now I get an error for the first line that
says:

unreported excpeption java.io.FileNotFoundException; must be caught or
declared to be thrown

I get the same error for the last line.  Ok, not sure what that meant so I
googled and came up with this:

       try{
    FileInputStream fs = new FileInputStream(f);
       }
       catch(FileNotFoundException fnfE){
           System.err.println("File not found!");
       }
    InputStreamReader sirLine;
    BufferedReader fileInput;
    sirLine = new InputStreamReader(fs);
    fileInput = new BufferedReader(sirLine);
    while ( j <= 53 ) {
        fileline = fileInput.readLine();

Ok, that made the original error go away and I think I understand why.
Seems like NetBeans is tying to make me write fault tolerant code, jerk.
However I have a new error that I don't understand.  The sirLine = new...
line gives me:

cannot find symbol
symbol : variable fs
location: class hockey_pool.Main

I have googled for this and cannot find what this means.  I thought that it
meant that I have not defined a variable, but I have in the line in the
try{}.  So, what am I missing here?

Thanks,

fybar
Monique Y. Mudama - 25 Apr 2006 19:03 GMT
[snip]

>         try{
>     FileInputStream fs = new FileInputStream(f);
[quoted text clipped - 21 lines]
> meant that I have not defined a variable, but I have in the line in the
> try{}.  So, what am I missing here?

The problem is that you declared it in the try block.  It's the same
problem as you would have if you declared a variable in an if block
and then tried to use it outside of that block.

The first thing I'd try to fix this is to put all the code that depends
on fs into the try block.

Signature

monique

Help us help you:
http://www.catb.org/~esr/faqs/smart-questions.html

fybar - 25 Apr 2006 19:35 GMT
> [snip]
>
[quoted text clipped - 30 lines]
> The first thing I'd try to fix this is to put all the code that depends
> on fs into the try block.

Thanks Monique.  I surrounded all of the relevent code with the try block
which then resulted in another error, which was an IOException.  Rinse and
repeat.  I got it working now but I have a few nested try blocks.  Is that
normal?

try{
FileInputStream fs = new FileInputStream(f);
InputStreamReader sirLine;
BufferedReader fileInput;
sirLine = new InputStreamReader(fs);
try{
fileInput = new BufferedReader(sirLine);

//MY WICKED AWESOME CODE HERE

}
catch(IOException IOE){
   System.err.println("Something is awry!");
}
}
catch(FileNotFoundException fnfE){
    System.err.println("File not found!");
}

Of course with the apropriately nested catch statements.  Is there a
cleaner way to do this?

Thanks for the help,

fybar
Monique Y. Mudama - 25 Apr 2006 20:02 GMT
> Thanks Monique.  I surrounded all of the relevent code with the try block
> which then resulted in another error, which was an IOException.  Rinse and
[quoted text clipped - 22 lines]
> Of course with the apropriately nested catch statements.  Is there a
> cleaner way to do this?

You can have one try block with multiple catch blocks following it.

try{}
catch (AException a){}
catch (BException b){}
etc.

By the way, I don't know what particularly you're trying to achieve,
but you almost certainly want to put something more substantive in
those catches.  If there's absolutely nothing your program can do when
it tries to open a nonexistant file, you might abort the whole app. If
it's a program that interacts with a user, you might give them an
opportunity to select a different file.  Etc.

Signature

monique

Help us help you:
http://www.catb.org/~esr/faqs/smart-questions.html

fybar - 25 Apr 2006 21:53 GMT
>> Thanks Monique.  I surrounded all of the relevent code with the try block
>> which then resulted in another error, which was an IOException.  Rinse and
[quoted text clipped - 29 lines]
> catch (BException b){}
> etc.

Ok, I will implement this.  I thought the nested try's were odd.

> By the way, I don't know what particularly you're trying to achieve,
> but you almost certainly want to put something more substantive in
> those catches.  If there's absolutely nothing your program can do when
> it tries to open a nonexistant file, you might abort the whole app. If
> it's a program that interacts with a user, you might give them an
> opportunity to select a different file.  Etc.

What?  My messages are perfect!  How dare you critisize my coding sytle!
:-P

I just put those messages in for the sake of brevity.  I will put more
meaningful messages and some error handling as well.  This particular app
does not have any interaction so I think I will quit with a message to a
log file in most cases.

Thanks again,

fybar
Oliver Wong - 25 Apr 2006 22:04 GMT
> This particular app
> does not have any interaction so I think I will quit with a message to a
> log file in most cases.

   Just as a heads up in case you don't know about it (since you seem a bit
new to Java), there's a built in logging mechanism, so you don't have to
design your own. See:

http://java.sun.com/j2se/1.5.0/docs/guide/logging/overview.html
http://java.sun.com/j2se/1.5.0/docs/api/java/util/logging/package-summary.html

   - Oliver
Amfur Kilnem - 25 Apr 2006 19:04 GMT
> New to java.  I have a snippet of code that I can compile fine on a
> FreeBSD
[quoted text clipped - 49 lines]
>
> fybar

Now Google for "block scope"


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.