이것은 클라이언트가 서로 채팅 할 수있게 해주는 내 서버 클래스이지만 while (!(line = in.readLine()).equalsIgnoreCase("/quit"))
도와 주시겠습니까? 감사합니다.null 포인터 예외 (서버 측)를 반환하는 이유
내 ChatHandler 클래스 :
final static Vector handlers = new Vector(10);
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ChatHandler(Socket socket) throws IOException {
this.socket = socket;
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(
new OutputStreamWriter(socket.getOutputStream()));
}
@Override
public void run() {
String line;
synchronized (handlers) {
handlers.addElement(this);
// add() not found in Vector class
}
try {
while (!(line = in.readLine()).equalsIgnoreCase("/quit")) {
for (int i = 0; i < handlers.size(); i++) {
synchronized (handlers) {
ChatHandler handler =
(ChatHandler) handlers.elementAt(i);
handler.out.println(line + "\r");
handler.out.flush();
}
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
synchronized (handlers) {
handlers.removeElement(this);
}
}
}
}
클라이언트 클래스의 일부 : 읽을 아무것도가없는 경우
String teXt = MainClient.getText();
os.println(teXt);
os.flush();
try {
String line = is.readLine();
setFromServertext("Text recieved:"+line+"\n");
is.close();
is.close();
c.close();
} catch (IOException ex) {
Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
}
당신이 게시 한 이전에'NullPointerException'에 관한 한 움큼의 질문을 던졌고 앞으로 어떻게 똑똑한 방법을 묻는 지 알려 주었고 또한 디버깅 및 마무리 방법에 대해 상당히 높은 수준으로 설명되었습니다 근본 원인. 어떤 식 으로든 배웠습니까? 예를 들어'in.readLine()'은'.equalsIgnoreCase()'가 전혀 작동하지 않도록'null'을 반환 할 수 있습니다. – BalusC