cannot find symbol

Discussion in 'Java' started by fybar, Apr 25, 2006.

  1. fybar

    fybar Guest

    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
     
    fybar, Apr 25, 2006
    #1
    1. Advertising

  2. fybar

    Amfur Kilnem Guest

    "fybar" <> wrote in message
    news:...
    > 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"
     
    Amfur Kilnem, Apr 25, 2006
    #2
    1. Advertising

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

    --
    monique

    Help us help you:
    http://www.catb.org/~esr/faqs/smart-questions.html
     
    Monique Y. Mudama, Apr 25, 2006
    #3
  4. fybar

    fybar Guest

    On Tue, 25 Apr 2006 12:04:17 -0600, Monique Y. Mudama wrote:

    > 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
     
    fybar, Apr 25, 2006
    #4
  5. On 2006-04-25, fybar penned:
    >
    > 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.

    --
    monique

    Help us help you:
    http://www.catb.org/~esr/faqs/smart-questions.html
     
    Monique Y. Mudama, Apr 25, 2006
    #5
  6. fybar

    fybar Guest

    On Tue, 25 Apr 2006 13:02:02 -0600, Monique Y. Mudama wrote:

    > On 2006-04-25, fybar penned:
    >>
    >> 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.


    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
     
    fybar, Apr 25, 2006
    #6
  7. fybar

    Oliver Wong Guest

    Oliver Wong, Apr 25, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny
    Replies:
    1
    Views:
    4,703
    Andrew Thompson
    May 26, 2004
  2. Replies:
    5
    Views:
    6,661
    Tilman Bohn
    Dec 16, 2004
  3. luch
    Replies:
    5
    Views:
    6,363
  4. Vojta
    Replies:
    1
    Views:
    492
    Simon
    Oct 11, 2006
  5. Replies:
    6
    Views:
    1,955
Loading...

Share This Page