안녕하세요 저는 벡터 및 스레드를 사용하여 채팅 서버에서 코드를 작성하고 있지만이 오류가 발생하고 이유를 모르겠습니다.서버의 java.lang.NullPointerException
오류가 발생하는 위치 : 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;
}
}
도움 :
이 내 코드?
받고있는 오류 메시지/예외를 추가하십시오. – Alexander
어떤 서버입니까? 번역? 스택 추적을 추가하십시오. –
안녕하세요. 귀하의 코드는이 시간에 검사하여 쉽게 디버깅 할 수 있었지만 향후 게시물에 대해서는 _complete_ 스택 추적 (코드 형식)을 게시하고 예외가 가리키는 코드에 해당 라인을 표시하십시오. 예외가 라이브러리 코드에있는 경우 코드에서 _is_ 스택 맨 위에 가장 가까운 점을 찾고 _that_ 행을 식별합니다. 누군가 당신에게 문제를 제기했는지 묻는 다른 정보를 포함 시키십시오. –