0

您好,我正在使用向量和线程为聊天服务器编写代码,但出现此错误,我不知道为什么?

错误发生在:ClientList.add(new Personnel(nekname,client));

这是我的代码:

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;
    }
 }

有什么帮助吗?

4

3 回答 3

2

ClientList也在你的ClientHandler类中初始化。

public static Vector<Personnel> ClientList
                                = new Vector<Personnel>(); // initialization MISSING!
于 2013-07-06T19:18:18.500 回答
2

你没有初始化你ClientListClientHandler课堂。

 public static Vector<Personnel> ClientList;
于 2013-07-06T19:20:52.753 回答
2

例外的原因是您在课堂上声明ClientList但从未声明过。initializedClientHandler

于 2013-07-06T19:21:10.130 回答