• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 SimpleServer.java
SimpleServer.java:20: 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
dos.close();
s1.close();
} catch (IOException e) { }
} //end of while
}//end of main
}//end of class
Thanks alot
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ayman Jaffar
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Carl Trusiak my program "SimpleServer.java" 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 "SimpleClient.java" and "SimpleServer.java" and they're both workin' fine.
Thanks again.

[This message has been edited by Ayman Jaffar (edited January 09, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic