(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.
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.
capitalizedSentence = clientSentence.toUpperCase() + '\n'; // Should be here.When "inFromClient.readLine()" reach the end of stream, it will return null.So "clientSentence" could be null!