私は単純なクライアントをコーディングしていますが、常に次のようになりますNullPointerException
。
while ((input = in.readLine()) != null) {
これが私のコードです:
public class ClientTest {
public static String server = "127.0.0.1";
public static int sport = 11111;
public static int cport = 11111;
private static String clientname="";
public static ExecutorService pool = Executors.newCachedThreadPool();
public static BufferedReader in = null;
public static PrintWriter out = null;
public static Socket socket = null;
public ClientTest(){
this.clientname="";
}
public static void main(String[] args) {
try {
socket = new Socket(server, sport);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch(IOException e) {
System.out.println("could not create connection:\n" + e);
try {
if(socket != null) socket.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
pool.execute(new Communicator());
}
public static class Communicator implements Runnable {
@Override
public void run() {
String input = "";
try {
while ((input = in.readLine()) != null) {//here i get the NPException
StringTokenizer tokenizer = new StringTokenizer(input);
tokenizer.nextToken();
if(input.startsWith("!open")) {
if(tokenizer.hasMoreTokens()) System.out.print(tokenizer.nextToken() + "> ");
else System.out.print("> ");
}
else if(input.startsWith("!exit")) {
System.out.print("Hallo hallo");
}
}
} catch(IOException e) {
System.out.println("An error as occurred while reading from server - \n" + e);
}
}
}
}
を取得するのはなぜNullPointerException
ですか? 私はまだ文字列入力を初期化しています。