0

I created a custom class for sockets:

class clientSocket{

    Socket clientSocket = null;

    public clientSocket() {

    }

    void accept(ServerSocket welcomeSocket) throws IOException{
        clientSocket = welcomeSocket.accept();
    }
}

When I try to use it in my server class like below, it throws a null pointer exception:

ServerSocket sSocket = new ServerSocket(1234);
clientSocket[] client = new clientSocket[3];
client[0].accept(sSocket); //this throws a null pointer exception.

Any idea why the 3rd line throws that null pointer?

Adib
  • 359
  • 1
  • 6
  • 16
  • 4
    When you initialize a new array you do not initialize its content. So you need to first use `client[0] = new ClientSocket()`. Also please note that classes are capitalized by convention. – Ben Jul 05 '18 at 10:38
  • @Ben Thanks, that was the solution. – Adib Jul 05 '18 at 10:43

0 Answers0