How to solve java.lang.NullPointerException?

3

I created the class Server ! However, an exception of type java.lang.NullPointerException is thrown! Could anyone 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; 
                }
            }
        }
    }
}
    
asked by anonymous 15.01.2015 / 02:17

1 answer

5

Your error occurs here:

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

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

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

And there will be no more NullPointerException .

There is still a problem that one of the sockets for the client will terminate without ever sending a quit , and thus it would get stuck in the internal loop because br.readLine() would always return null . The solution is to test null on the second if :

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

There are other things you can do to improve your code. It is possible to eliminate condition variables by using break or return instead of condition = false . The same can apply to condition1 , where you can transform your% internal% to while . Also you can avoid having to use that% awful% with try-with-resources . Your code looks 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 too complicated, you can use finally :

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