0

Whenever I run my Chat Server it works until the client connects and then I get a Null Pointer exception at connections.add(socket); which crashes it and then this happens every time I run it. I know my client side is fine because I watched a tutorial a few months ago on how to do this and that server works with my client without crashing

private static ServerSocket server;
private static Socket socket;
private static Scanner input;
private static PrintWriter output;

private static final int port = 444;
private static String message;
private static ArrayList<Socket> connections;
private static Server serverClass;

public Server() {

    message = "";
    connections = new ArrayList<Socket>();
    serverClass = new Server();

}

public void run() {

    try {

        try {

            input = new Scanner(socket.getInputStream());
            output = new PrintWriter(socket.getOutputStream());

            while(true) {

                checkConnection();

                if(!input.hasNext()) {

                    return;

                }

                message = input.nextLine();
                System.out.println("Client said: " + message);

                for(int i = 1; i <= connections.size(); i++) {

                    Socket tempSock = (Socket) connections.get(i - 1);
                    output.println(message);
                    output.flush();

                }

            }

        }
        finally {

            socket.close();

        }

    }
    catch(Exception e) {

        System.out.print(e);

    }

}

public void checkConnection() throws IOException {

    if(!socket.isConnected()) {

        for(int i = 1; i <= connections.size(); i++) {

            if(connections.get(i) == socket) {

                connections.remove(i);

            }

        }

        for(int i = 1; i <= connections.size(); i++) {

            Socket disconnectedUser = (Socket) connections.get(i - 1);
            System.out.println(disconnectedUser + " has Disconnected!");

        }

    }

}

public static void main(String[] args) throws IOException{

    try {

        server = new ServerSocket(port);
        System.out.println("Waiting for Clients... ");

        while(true) {

            socket = server.accept();
            connections.add(socket);
            System.out.println("Client connected from: " + socket.getLocalAddress().getHostName());

            Thread thread = new Thread(serverClass);
            thread.start();

        }

    }
    catch(Exception e) {

        e.printStackTrace();

    }

}
2
  • did you debug and see if there are any values in connections? Commented Aug 19, 2013 at 0:36
  • 1
    @mdoran3844 He is accepting the socket, not connecting it. Unless you are incorrectly asserting that ArrayList cannot have null elements, your question is irrelevant. Commented Aug 19, 2013 at 1:19

1 Answer 1

3

If you get an NPE at connections.add(socket), it can only be because connections is null. Which it is, because you haven't constructed an instance of Server: instead you are trying to execute all its code from main(). The only place you call new Server() from is inside the constructor for Server, which you have clearly never executed at all, as you would have got a StackOverflowError from the infinite recursion.

This code is a real mess:

  • fix the infinite recursion
  • construct an instance of Server in your main() method
  • make connections non-static
  • make socket a local variable in the accept loop
  • fix your loops to iterate from 0 to connections.size()-1

and try again.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.