Need help figuring out scope in Java

Discussion in 'Java' started by rboelio, Feb 3, 2004.

  1. rboelio

    rboelio Guest

    I'm having what I think boils down to a problem with scope in Java.
    I'm working on a small server for my networks class, and am fairly new
    to the language. I've got some code to open a socket, and parse the
    first line of text received for the pathname of a file I want to send
    back through the socket. Once the path is received, I'm trying to use
    a FileReader to extract text from the file, a BufferedReader to read
    it and a DataOutputStream to send it. I'm a bit confused as to why I
    need so many streams and readers, etc, but I digress. Here is a bit
    of code, with some more details below it.

    while(( clientLine = inFromClient.readLine() ) != null){
    if(lineCount == 0){
    int d1 = clientLine.indexOf(" ");
    int d2 = clientLine.indexOf(" ", d1+1);
    String path = new String(clientLine.substring(d1+1, d2));
    fin = new BufferedReader(new FileReader(args[0] + path));
    lineCount++;
    }

    String line;
    while(( line = fin.readLine() ) != null){
    outToClient.writeBytes(line);
    }

    }

    The problem comes with the declaration and instantiation of the
    variable fin. Currently, I've got this line:

    BufferedReader fin = new BufferedReader(null);

    At the top of main(). That line is causing runtime errors, I presume
    because of the null parameter. However, I've got nothing else to pass
    the constructor yet because the socket isn't even opened. However,
    when I try this line in the outer while loop pasted above:

    BufferedReader fin = new BufferedReader(new FileReader(args[0] +
    path);

    where " args[0] + path" contains the correct pathname, the compiler
    complains about subsequent lines that attempt to read from fin (the
    inner while loop) and close the stream. I get errors saying fin may
    not have been instantiated. I assume this is because it is possible
    that fin may never get instantiated within the conditional if(),
    although I have added some lines to prevent that. Any help would be
    much appreciated, as this is only the second of a 3 part lab due on
    Thursday :)
     
    rboelio, Feb 3, 2004
    #1
    1. Advertising

  2. "rboelio" ...
    > I'm having what I think boils down to a problem with scope in Java.


    It is more difficult to explain because
    you did not supply self-contained,
    complete example.
    [ http://www.physci.org/codes/sscce.jsp ]

    That aside, check
    http://www.physci.org/launcher.jsp#SampleChoice

    In this example I declare the Choice (at line 11)
    as a class variable. _Any_ method within that
    class can access it.

    Note also that I do not _instantiate_ it until the
    constructor (line 24).

    You should be able to figure out where you
    are going wrong by looking closely at that
    example, but if not, prepare an SSCCE.

    HTH

    --
    Andrew Thompson
    * http://www.PhySci.org/ Open-source software suite
    * http://www.PhySci.org/codes/ Web & IT Help
    * http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Feb 3, 2004
    #2
    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. darrel
    Replies:
    19
    Views:
    601
    darrel
    Jan 30, 2006
  2. Dale
    Replies:
    4
    Views:
    368
  3. Nathan Pinno
    Replies:
    8
    Views:
    408
    Brian
    Jun 29, 2005
  4. JackBlack
    Replies:
    1
    Views:
    305
    JackBlack
    Sep 26, 2006
  5. BlackHelicopter
    Replies:
    1
    Views:
    585
    eliascm
    Jun 3, 2010
Loading...

Share This Page