title: Java ServerSocket exception when no data is sent
body: <p>I have simple <code>Daemon</code> setup with a ServerSocket that listens for a packet, when a connection is made and data is sent, everything works fine, but when a connection is made and NO data is sent, the program crashes with an exception. Here is the code for the <code>Listener</code> thread.</p>
  
  <pre><code>public class Listener implements Runnable {
      ServerSocket listenerSocket;
      Socket connection = null;
      OutputStream out;
      BufferedReader in;
      String packet;
      Console console;
      public Listener(Console Console){
          console = Console;
  
      }
      @Override
      public void run()
      {
          try{
              listenerSocket = new ServerSocket(5344); //General socket setup
              console.write("INFO", "Listener started");
              do{
              connection = listenerSocket.accept();
              BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
              DataOutputStream out = new DataOutputStream(connection.getOutputStream()); 
                  try{
                      packet = (String)in.readLine(); // Get packet data
                      if(packet != null){
                          byte[] decoded = Base64.decodeBase64(packet);
                          packet = new String(decoded, "UTF-8");
                          Thread handler = new Thread(new Handler(packet, out, console, in, connection)); // Handler class will take / parse packet data
                          handler.start();
                      }
                  }
                  catch(Exception e){
                      e.printStackTrace();
                  }
              }while(!packet.equals("shutdown"));
          }
          catch(IOException ioException){
              ioException.printStackTrace();
          }
          finally{
              try{
                  in.close();
                  out.close();
                  listenerSocket.close();
              }
              catch(IOException ioException){
                  ioException.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>This is the error when a connection is made but no data is sent:</p>
  
  <pre><code>Exception in thread "Thread-0" java.lang.NullPointerException
          at com.cj.panel.Listener.run(Listener.java:53)
          at java.lang.Thread.run(Thread.java:744)
  </code></pre>
  
  <p>Line 53 is <code>in.close();</code></p>