uninitialized local variable error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stmfc
    New Member
    • May 2007
    • 65

    uninitialized local variable error

    in the following code, i got
    the error "local variable s may not have been initialized" on line 19
    but i think that ServerSocket variable s is obviously initialized on line 6.
    what can be the problem?


    Code:
    import java.net.*;
    import java.io.*;
    
    public class SimpleServer {
    public static void main(String args[]) {
    ServerSocket s;
    
    // Register your service on port 5432
    try {
     s = new ServerSocket(5432);
     } catch (IOException e) {
     // ignore
     }
    
     // Run the listen/accept loop forever
     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) {
     // ignore
     }
     }
     }
     }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    No, there is a possiblity that 's' isn't initialized; supposed an exception is thrown
    (which you ignore); what would the value of 's' be then?

    kind regards,

    Jos

    Comment

    • sateesht
      New Member
      • Apr 2007
      • 41

      #3
      Initialize the ServerSocket Object as below:

      Serversocket s=null;


      Cheers,
      Sateesh.

      Comment

      Working...