带有ServerSockets的Java Thread NullPointerException

时间:2013-10-07 22:35:55

标签: java nullpointerexception serversocket

我正在尝试让这段代码在不同的端口上运行5个独立的SocketServers。我从线程中获取NullPointerExceptions但我不明白为什么。一些帮助会很棒。

第72行是服务器的接受方法:

  

套接字套接字= 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)

1 个答案:

答案 0 :(得分:3)

ServerSocket server = null构造函数中的

TCPSocketServer(int port)正在影响ServerSocket server成员。从构造函数中取出一个,你应该很高兴。