2013-07-06 5 views
0

안녕하세요 저는 벡터 및 스레드를 사용하여 채팅 서버에서 코드를 작성하고 있지만이 오류가 발생하고 이유를 모르겠습니다.서버의 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; 
    } 
} 

도움 :

이 내 코드?

+0

받고있는 오류 메시지/예외를 추가하십시오. – Alexander

+0

어떤 서버입니까? 번역? 스택 추적을 추가하십시오. –

+0

안녕하세요. 귀하의 코드는이 시간에 검사하여 쉽게 디버깅 할 수 있었지만 향후 게시물에 대해서는 _complete_ 스택 추적 (코드 형식)을 게시하고 예외가 가리키는 코드에 해당 라인을 표시하십시오. 예외가 라이브러리 코드에있는 경우 코드에서 _is_ 스택 맨 위에 가장 가까운 점을 찾고 _that_ 행을 식별합니다. 누군가 당신에게 문제를 제기했는지 묻는 다른 정보를 포함 시키십시오. –

답변

2

ClientHandler 클래스에서 ClientList을 초기화하십시오.

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

그냥 그렇게 ... 고마워요. –

+0

@senderellaali 도움이되기를 기뻐합니다. 이 질문에 대한 답변을 수락하십시오. 감사. –

2

당신은 당신의 ClientListClientHandler 클래스를 초기화하지 않았다. 예외

public static Vector<Personnel> ClientList; 
2

이유는 ClientList을 선언하지만 결코 initialized 그 클래스 ClientHandler에 있다는 것입니다.

관련 문제