0

I am coding a simple client, but I always get a NullPointerException at:

            while ((input = in.readLine()) != null) {

Here is my code:

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);
        }
    }

}


}

Why do I get the NullPointerException? I am still initializing the String input.

3
  • 1
    you should add where you are getting null pointer exception or total error message Commented Oct 16, 2012 at 20:31
  • check what the value of in is ... chances are it is what's giving you the nullpointer exception. Commented Oct 16, 2012 at 20:31
  • Do you get a message says ' could not create connection: ...' If yes, socket itself not connected. Commented Oct 17, 2012 at 11:44

2 Answers 2

7

My question is, why do I get the NP Exception, I am still initializing the String input!

You would get a NullPointerException there if in is null... which it certainly could be. After all, if anything in the first part of main throws an exception, you catch it, print it out... and then keep going! So if socket.getOutputStream() throws an exception, both in and out will be null.

Moral: catching an exception and just continuing as if nothing had happened is very rarely a good idea.

Sign up to request clarification or add additional context in comments.

1 Comment

@maximus: You only initialize it if an exception isn't thrown by earlier statements...
0

I think something in the try block is failing and in is not being initialized, then after the exception you are running Comunicator and in is still null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.