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));

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


    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] +

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

  2. "rboelio" ...
    It is more difficult to explain because
    you did not supply self-contained,
    complete example.
    [ ]

    That aside, check

    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.

    Andrew Thompson, Feb 3, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.