cannot find symbol

F

fybar

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
 
A

Amfur Kilnem

fybar said:
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

Now Google for "block scope"
 
M

Monique Y. Mudama

On 2006-04-25, fybar penned:

[snip]
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?

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

fybar

On 2006-04-25, fybar penned:

[snip]
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?

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.

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
 
M

Monique Y. Mudama

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?

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

fybar

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

try{}
catch (AException a){}
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
 
O

Oliver Wong

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top