How to resolve java.lang.Nullpointerexception?

Asked

Viewed 57,092 times

4

I created the class Server! However an Exception of the type is launched java.lang.NullPointerException! I wonder if someone could help me with this problem?

The class is as follows:

public class Server {

    public static final int SERVERPORT = 8888;
    public static void main(String[] args) throws IOException {



        System.out.println("Starting echo server...");
        ServerSocket ss = new ServerSocket(SERVERPORT);

        boolean condition = true;

        while (condition)
        {
            Socket s = ss.accept();

            try
            {
                InputStream is = s.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);

                String msg = br.readLine();

                boolean condition1 = true;
                while (condition1) {

                    System.out.println(msg);
                    msg = br.readLine();

                    if(msg.equals("quit")){
                        condition1=false;
                    }
                }

                if(msg.equals("quit")){
                    condition=false;
                }

            }
            catch (IOException ioe)
            {
                System.err.println("I/O error: " + ioe.getMessage());
            }
            finally
            {
                try
                {
                    s.close();
                }
                catch (IOException ioe)
                {
                    assert false; 
                }
            }
        }
    }
}
  • 2

    Where the error comes?

  • The error points to the following statement: if(msg.equals("quit"))

  • 2

    @Marcoaf If the stream content ends before the message quit arrive, the br.readLine() will return null. There’s not much of a way out except test it out... (or make sure there’s never gonna miss the quit at the end) P.S. You read the message, print it on the screen, then read it again - how to know if the first message received was not quit?

  • 2

    I won’t be able to help you but since I’ve modified the code so I can better understand the hint of a logic without flags. http://ideone.com/aiXooY

  • 1

    @bigown -> http://meta.pt.stackoverflow.com/questions/2333/por-que-muitas-peoples-respondsnos-coment%C3%A1rios-in-place-to-create-a-response - the tip :)

  • @Victor doesn’t believe this answers the question. I don’t have Java here to test correctly.

Show 1 more comment

1 answer

5


Your error occurs here:

if(msg.equals("quit")){

Actually, there are two lines like this. One simple way to solve this is:

if("quit".equals(msg)) {

And there will be no more NullPointerException.

There is still the problem of one of the sockets for the client to end without ever sending one quit, and with it he would be caught in the inner loop because br.readLine() would always return null. The solution is to test the null in the second if:

if(msg == null || "quit".equals(msg)) {

There are other things that can be improved in your code. It is possible to delete variables condition when using a break or return instead of condition = false. The same may apply to condition1, in which it is possible to transform your while internal in a for. Also you can avoid having to use that finally horrible with Try-with-Resources. Your code goes like this:

public class Server {

    public static final int SERVERPORT = 8888;
    public static void main(String[] args) throws IOException {

        System.out.println("Starting echo server...");
        ServerSocket ss = new ServerSocket(SERVERPORT);

        loop: while (true) {
            Socket s = ss.accept();

            try (InputStream is = s.getInputStream()) {
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);

                for (String msg = br.readLine(); msg != null; msg = br.readLine()) {
                    System.out.println(msg);
                    if ("quit".equals(msg)) break loop;
                }
            } catch (IOException ioe) {
                System.err.println("I/O error: " + ioe.getMessage());
            }
        }
    }
}

And if you find this for very complicated, can use a do-while:

                String msg;
                do {
                    msg = br.readLine();
                    System.out.println(msg);
                    if ("quit".equals(msg)) break loop;
                } while (msg != null);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.