2013-10-07 55 views
0

我試圖讓此代碼在單獨的端口上運行5個單獨的SocketServers。我從線程獲取NullPointerExceptions,但我不明白爲什麼。一些幫助會很好。Java線程與ServerSockets的NullPointerException

線72是服務器的接受方法:

Socket套接字= server.accept();

public class TCPSocketServer { 

    /** 
    * Accept this many connections. 
    */ 
    private int my_backlog = 5; 

    /** 
    * The server socket. 
    */ 
    private ServerSocket server = null; 

    private static final int SERVICE_DISCOVERY_PORT = 1234; 
    private static final int ADDITION_SERVICE_PORT = 1235; 
    private static final int DIVISION_SERVICE_PORT = 1236; 
    private static final int MULTIPLICATION_SERVICE_PORT = 1237; 
    private static final int SUBTRACTION_SERVICE_PORT = 1238; 

    final static int[] SERVICE_PORTS = { 
           SERVICE_DISCOVERY_PORT, 
           ADDITION_SERVICE_PORT, 
           DIVISION_SERVICE_PORT, 
           MULTIPLICATION_SERVICE_PORT, 
           SUBTRACTION_SERVICE_PORT 
          }; 

    /** 
    * Create the server socket. 
    * @param a_port the port that the server socket should listen on. 
    */ 
    public TCPSocketServer(int port) { 

     ServerSocket server = null; 

      try { 
       server = new ServerSocket(port, my_backlog); 

       System.out.println("Service discovery listening on port " + port); 

      } catch (IOException ioe) { 
       ioe.printStackTrace(); 
      } catch (SecurityException se) { 
       se.printStackTrace(); 
      } 
    } // end TCPSocketServer method 


    public void listen() { 

     new Thread() { 
      public void run() { 
       while (true) {    
        try { 
         Socket socket = server.accept(); 

         DataInputStream in = new DataInputStream(socket.getInputStream()); 
         double dividend = in.readDouble(); 
         double divisor = in.readDouble(); 

         DataOutputStream out = new DataOutputStream(socket.getOutputStream()); 
         double result = 0; 
         result = dividend/divisor; // TODO: check for zero divisor 
         out.writeDouble(result); // TODO: separate into divide() method 
         out.flush(); 

         // tidy up 
         in.close(); 
         out.close(); 
         socket.close(); 

        } catch (IOException ioe) { 
         ioe.printStackTrace(); 
        } catch (SecurityException se) { 
         se.printStackTrace(); 
        } // end try/catch 
       } // end while loop 
      } // end run() 
     }.start(); // end Thread 
    } // end listen() 




// the main method 
    public static void main(String[] args) { 
     int port = 0; 

      for(int servicePort : SERVICE_PORTS){ 
       // Create the server 
       TCPSocketServer socketServer = new TCPSocketServer(servicePort); 
       // Listen on the server socket. This will run until the program is 
       // killed. 
       socketServer.listen(); 
      } 

    } // end main 
} 

下面是一些示例輸出:

Service discovery listening on port 1234 
Exception in thread "Thread-0" java.lang.NullPointerException 
    at TCPSocketServer$1.run(TCPSocketServer.java:72) 
Service discovery listening on port 1235 
Exception in thread "Thread-1" java.lang.NullPointerException 
    at TCPSocketServer$1.run(TCPSocketServer.java:72) 
Service discovery listening on port 1236 
Exception in thread "Thread-2" Service discovery listening on port 1237java.lang.NullPointerException 
    at TCPSocketServer$1.run(TCPSocketServer.java:72) 

Exception in thread "Thread-3" Service discovery listening on port 1238 
java.lang.NullPointerException 
    at TCPSocketServer$1.run(TCPSocketServer.java:72) 
Exception in thread "Thread-4" java.lang.NullPointerException 
    at TCPSocketServer$1.run(TCPSocketServer.java:72) 
+2

請在您的服務器代碼中確定第**行** **。 –

+0

我把它粘貼到一個編輯器中,通過我的推算,L72是匿名的'Thread'對象中的'ioe.printStackTrace()',它不能爲空。 – Faelkle

回答

3

ServerSocket server = nullTCPSocketServer(int port)構造函數陰影的ServerSocket server會員上漲。從你的構造函數中取出一個,你應該很好走。

+1

然後用數據流修復重複的關閉問題。它會在這個問題得到解決之後拋出異常:)。 –

+1

你知道,自從我用Java編寫套接字已經很長時間了,但我記得實際上是在一個線程中執行'accept',然後產生新的線程來處理返回的'Socket's。我懷疑這個實現實際上是單線程的,這可能不是作者想要的(但不是他們要求的;)*編輯*我把它取回來 - 它看起來像每個「SERVICE_PORT」的一個線程:) – Faelkle