Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Need help figuring out scope in Java

Reply
Thread Tools

Need help figuring out scope in Java

 
 
rboelio
Guest
Posts: n/a
 
      02-03-2004
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
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      02-03-2004
"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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help figuring out how to control this.. edge triggering. BlackHelicopter VHDL 1 06-03-2010 10:27 AM
Need help figuring this out please. =?Utf-8?B?bWlzc21vb24wMA==?= Microsoft Certification 1 05-07-2007 02:17 AM
Need help figuring out rendering issues... JackBlack ASP .Net 1 09-26-2006 06:23 PM
Help figuring out why my webpage refreshes to the homepage. Carrie76 General Computer Support 1 09-26-2006 01:11 AM
I need help figuring out how to fix this code. Nathan Pinno Python 8 06-29-2005 05:01 PM



Advertisments