服务器中的java.lang.NullPointerException

服务器中的java.lang.NullPointerException,java,nullpointerexception,Java,Nullpointerexception,您好,我正在使用向量和线程为聊天服务器编写代码, 但我得到这个错误,我不知道为什么 错误发生在:ClientList.add(新人员(nekname,client)) 这是我的代码: public class chatServer { private static ServerSocket serverSocket; private static final int PORT = 5002; public static Vector<Personnel> Cli

您好,我正在使用向量和线程为聊天服务器编写代码, 但我得到这个错误,我不知道为什么

错误发生在:ClientList.add(新人员(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;
    }
 }
公共类聊天服务器{
私有静态ServerSocket ServerSocket;
专用静态最终int端口=5002;
公共静态向量ClientList;
公共静态void main(字符串[]args)引发IOException
{ 
试试{
serverSocket=新的serverSocket(端口);
ClientList=新向量();
} 
捕获(IOException ioEx){
System.out.println(“\n无法设置端口!”);
系统出口(1);
} 
做{
Socket client=serverSocket.accept();
System.out.println(“\n接受新客户端。\n”);
ClientHandler=新的ClientHandler(客户端);
handler.start();
}虽然(正确);
} 
} 
类ClientHandler扩展线程
{ 
专用套接字客户端;
专用扫描仪输入;
专用打印机输出;
公共静态向量ClientList;
公用客户端句柄(套接字){
客户端=套接字;
试试{
输入=新扫描仪(client.getInputStream());
输出=新的PrintWriter(client.getOutputStream(),true);
} 
捕获(IOException ioEx){
ioEx.printStackTrace();
} 
} 
public void run(){
收到字符串;
弦内克纳姆;
nekname=input.nextLine();
添加(新人员(内克南、客户));
试一试{
对于(人员:客户名单){
PrintWriter out=新的PrintWriter(person.getLink().getOutputStream(),true);
println(nekname+“已进入聊天室”);
}
}
捕获(IOException ioEx){
ioEx.printStackTrace();
}
做{
received=input.nextLine();
试一试{
对于(人员:客户名单){
PrintWriter out=新的PrintWriter(person.getLink().getOutputStream(),true);
out.println(nekname+“:”+已接收);
}
}
捕获(IOException ioEx){
ioEx.printStackTrace();
} 
}而(!received.equals(“拜拜”)| |!received.equals(“拜拜”);
尝试{if(client!=null){
对于(人员:客户名单){
PrintWriter out=新的PrintWriter(person.getLink().getOutputStream(),true);
println(nekname+“已离开聊天室”);
}
System.out.println(“关闭连接…”);
client.close();}
} 
捕获(IOException ioEx){
System.out.println(“无法断开连接!”);
} 
} 
}
班级工作人员{
私有字符串昵称;
专用套接字链路;
公共人员(字符串名称,套接字l){
昵称=姓名;
link=l;
}
公共字符串getName(){
返回昵称;
}
公共套接字getLink(){
返回链接;
}
}

有什么帮助吗?

ClientHandler
类中初始化
ClientList

public static Vector<Personnel> ClientList
                                = new Vector<Personnel>(); // initialization MISSING!
publicstaticvectorclientlist
=新向量();//初始化丢失!

ClientHandler
类中初始化
ClientList

public static Vector<Personnel> ClientList
                                = new Vector<Personnel>(); // initialization MISSING!
publicstaticvectorclientlist
=新向量();//初始化丢失!

您没有在
ClientHandler
类中初始化
ClientList

 public static Vector<Personnel> ClientList;
公共静态向量ClientList;

您没有在
ClientHandler
类中初始化
ClientList

 public static Vector<Personnel> ClientList;
公共静态向量ClientList;

出现异常的原因是您声明了
ClientList
,但从未在
ClientHandler
类中初始化它

出现异常的原因是您在
ClientHandler
类中声明了
ClientList
,但从未初始化它

请添加您收到的错误消息/异常。哪个服务器?版本添加stacktrace。欢迎使用SO。通过这次检查,您的代码很容易调试,但是对于以后的文章,请记住发布完整的堆栈跟踪(与代码的格式相同),并指出异常所指向的代码行。如果异常在库代码中,请找到代码中最靠近堆栈顶部的点,并标识该行。如果有人向您提出问题,请包括您需要的任何其他信息。不要忘记“接受”对您最有帮助的答案(单击答案旁边的复选标记)。请添加您收到的错误消息/异常。什么服务器?版本添加stacktrace。欢迎使用SO。通过这次检查,您的代码很容易调试,但是对于以后的文章,请记住发布完整的堆栈跟踪(与代码的格式相同),并指出异常所指向的代码行。如果异常在库代码中,请找到代码中最靠近堆栈顶部的点,并标识该行。如果有人向您提出问题,请提供您需要的任何其他信息。不要忘记“接受”对您最有帮助的答案(单击答案旁边的复选标记)。@senderellaali很高兴能为您提供帮助。请接受答案以标记此问题已结束。谢谢。@senderellaali很高兴能帮上忙。请接受答案以标记此问题已结束。谢谢