File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes s.accept ( ) method, gives me an error ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "s.accept ( ) method, gives me an error ?" Watch "s.accept ( ) method, gives me an error ?" New topic

s.accept ( ) method, gives me an error ?

Ayman Jaffar
Ranch Hand

Joined: Sep 06, 2000
Posts: 57
Hi all,
I've a win98 OS and I don't recal installing any S/W for server.
I've got this program from a book and I want to know why it's not working.
I'm getting this message exactly:
C:\Delete>javac variable s might not have been initialized
Socket s1 = s.accept();
1 error

and here is my program:
public class SimpleServer {
public static void main(String args[]) {
ServerSocket s;
// Register service to port 5432 or anyother number
try {
s = new ServerSocket(5432);
} catch (IOException e) { }

while (true) {
try {
// Wait here and listen for a connection
Socket s1 = s.accept();
// Get output stream associated with the socket
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(s1out);
// Send your string!
dos.writeUTF("Hello Net World!");
// Close the connection, but not the server socket
} catch (IOException e) { }
} //end of while
}//end of main
}//end of class
Thanks alot

Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
Which Version of the JDK are you using? The problem appears to be a condition called something like self referential error. It's a documented bug with certain versions. Basically the compiler sees that s may not get itialized in the constructor because it exists inside of braces and it complains. set s to null when you declare it
ServerSocket s = null;
or in the catch statement of the constructor prevents this.
One note, never have an empty catch block, this will mask errors and can be a horror to debug. a simple e.printStackTrace() will save a lot of hair!

Hope This Helps

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Ayman Jaffar
Ranch Hand

Joined: Sep 06, 2000
Posts: 57
Thank you Carl Trusiak my program "" is now working becuase I set "ServerSocket s = null;"....exactly as you said !!
I've jdk 1.3 and I guess it is a bug.
I've just finished runnig both "" and "" and they're both workin' fine.
Thanks again.

[This message has been edited by Ayman Jaffar (edited January 09, 2001).]
I agree. Here's the link:
subject: s.accept ( ) method, gives me an error ?
It's not a secret anymore!