exception in thread main java.lang.nullpointerexception

mangoleo 2012-02-21 11:46:01
(Sorry guys, I dont have chinese input on this machine)
The following simple code can compile, but give the following error message after execution:

exception in thread main java.lang.nullpointerexception
at TCPserver.main(TCPServer.java:20)


import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence = "";
String capitalizedSentence = "";
ServerSocket welcomeSocket = new ServerSocket(6789);

Socket connectionSocket = welcomeSocket.accept();

BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(
connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(
connectionSocket.getOutputStream());
while(true) {
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}


can someone help me identify what the problem is?

Many thanks.

...全文
124 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
MiceRice 2012-02-21
  • 打赏
  • 举报
回复
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n'; // Should be here.

When "inFromClient.readLine()" reach the end of stream, it will return null.

So "clientSentence" could be null!

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧