Tutorial :Multithreaded chat-server in Java


I'm trying to implement a server-client socket program in Java that can support multiple clients, but my class that performs the multithreading always crashes whenever my client connects to my server.

import java.io.*;  import java.net.*;  public class ClientWorker extends Thread{      Socket cwsocket=null;        public ClientWorker(Socket cwsocket){          super("ClientWorker");          cwsocket=cwsocket;      }        public void run(){          try {              PrintWriter out = new PrintWriter(cwsocket.getOutputStream(), true);              BufferedReader in = new BufferedReader(new InputStreamReader(cwsocket.getInputStream()));                String serverinput, serveroutput="";                out.println(serveroutput);                while ((serverinput = in.readLine()) != null) {                  out.println(serveroutput);                  if (serveroutput.equals("Terminate"))                      break;              }              out.close();              in.close();              cwsocket.close();            } catch (IOException e) {              e.printStackTrace();          }         }  }  

Whenever I create a PrintWriter object, a NullPointerException exception is thrown, and I'm not sure why it continues to happen. Below are my server and client classes. What am I doing wrong?

import java.io.*;  import java.net.*;  public class Server {        public static void main(String[]args)throws IOException{          ServerSocket serversocket=null;          final int PORT_NUM=4444;          boolean flag=true;          try{              System.out.println("Listening for connection");              serversocket=new ServerSocket(PORT_NUM);          }catch(IOException e){              System.out.println("Could not listen to port: "+PORT_NUM);              System.exit(-1);              }          while(flag){              new ClientWorker(serversocket.accept()).start();          }          System.out.println("Terminating server...");          serversocket.close();      }  }    import java.io.*;  import java.net.*;  public class Client {        public static void main(String[] args){          Socket socket=null;          PrintWriter out=null;          BufferedReader in=null;          BufferedReader userInputStream=null;          String IP="";          try{              socket = new Socket(IP, 4444);              out = new PrintWriter(socket.getOutputStream(), true);              in = new BufferedReader(new InputStreamReader(socket.getInputStream()));          } catch (UnknownHostException e) {              System.out.println("Unknown host:" + IP);              System.exit(1);          } catch  (IOException e) {              System.out.println("Cannot connect to server...");              System.exit(1);          }          String userInput, fromServer;          try{                      userInputStream = new BufferedReader(new InputStreamReader(System.in));              while ((fromServer = in.readLine()) != null) {                  System.out.println("Server: " + fromServer);                  if (fromServer.equals("Terminate"))                      break;                    userInput = userInputStream.readLine();                  if (userInput != null) {                          System.out.println("> " + userInput);                          out.println(userInput);                  }              }          }catch(IOException e){              System.out.println("Bad I/O");              System.exit(1);          }          try{              out.close();              in.close();              userInputStream.close();              socket.close();              System.out.println("Terminating client...");          }catch(IOException e){              System.out.println("Bad I/O");              System.exit(1);          }catch(Exception e){              System.out.println("Bad I/O");              System.exit(1);          }      }  }  



    public ClientWorker(Socket cwsocket){          super("ClientWorker");          cwsocket=cwsocket;      }  

You need to do


Or rename the parameter so it doesn't shadow the member of the same name.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »