senderella ali
senderella ali

Reputation: 11

java.lang.NullPointerException in Server

hello I'm writing a code fore a chat server using Vectors and threads, but i get this error and i don't know why?

the error occurs in : ClientList.add(new Personnel(nekname,client) );

this is my code:

public class chatServer { 

  private static ServerSocket serverSocket; 
  private static final int PORT = 5002; 

  public static Vector<Personnel> ClientList;

  public static void main(String[ ] args) throws IOException 
  { 
    try { 
      serverSocket = new ServerSocket(PORT);
      ClientList = new Vector<Personnel>();
    } 
    catch (IOException ioEx) { 
      System.out.println("\nUnable to set up port!"); 
      System.exit(1);
      } 
    do {
      Socket client = serverSocket.accept(); 
      System.out.println("\nNew client accepted.\n"); 
      ClientHandler handler = new ClientHandler(client); 
      handler.start();
    }while (true); 
  } 
} 
class ClientHandler extends Thread 
{ 
  private Socket client; 
  private Scanner input; 
  private PrintWriter output;

  public static Vector<Personnel> ClientList;

  public ClientHandler(Socket socket) { 
    client = socket; 
    try { 
      input = new Scanner(client.getInputStream()); 
      output = new PrintWriter( client.getOutputStream(),true); 
    } 
    catch(IOException ioEx) { 
      ioEx.printStackTrace(); 
    } 
  } 

  public void run() { 
    String received; 
    String nekname;

    nekname = input.nextLine();

    ClientList.add(new Personnel(nekname,client) );

    try{
      for(Personnel person:ClientList){
      PrintWriter out = new PrintWriter(person.getLink().getOutputStream(),true); 
      out.println(nekname + " has entered the chatroom");
      }
    }
   catch(IOException ioEx) { 
      ioEx.printStackTrace(); 
    }

    do {
      received = input.nextLine(); 
      try{
      for(Personnel person:ClientList){
      PrintWriter out = new PrintWriter( person.getLink().getOutputStream(),true); 
      out.println(nekname + ": " + received);
      }
      }
      catch(IOException ioEx) { 
      ioEx.printStackTrace(); 
    } 

    }while (!received.equals("Bye") || !received.equals("bye"));

    try { if (client!=null) {
      for(Personnel person:ClientList){
      PrintWriter out = new PrintWriter( person.getLink().getOutputStream(),true); 
      out.println(nekname + " has left the chatroom");
      }
      System.out.println( "Closing down connection...");
      client.close(); } 
    } 
    catch(IOException ioEx) { 
      System.out.println("Unable to disconnect!");
    } 
  } 
}

 class Personnel{
    private String nickname;
    private Socket link;

    public Personnel(String name,Socket l){
      nickname = name;
      link = l;
    }

    public String getName(){
      return nickname;
    }

     public Socket getLink(){
      return link;
    }
 }

any help?

Upvotes: 0

Views: 1882

Answers (3)

Addict
Addict

Reputation: 823

Reason for exception is that you declared ClientList but never initialized it in ClientHandler class.

Upvotes: 2

AllTooSir
AllTooSir

Reputation: 49352

You didn't initialize your ClientList in ClientHandler class.

 public static Vector<Personnel> ClientList;

Upvotes: 2

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Initialize ClientList in your ClientHandler class as well.

public static Vector<Personnel> ClientList
                                = new Vector<Personnel>(); // initialization MISSING!

Upvotes: 2

Related Questions