title: null pointer exception on handler for utility class being called from main class. sockets program Android
body: <p>I made two classes,  the first is Server class that runs a thread to wait for and accept a socket connection from the client.</p>
  
  <p>the second class is the EchoThread class that starts a new thread that gets the text message passed by the socket connection.  the reason for this is becase it is requied to do this so I can connect more than one client at a time to the server. so I created this class.  It is called in the while(true) loop in the Server class to start a new thread when a connection is made.</p>
  
  <p>the problem is that the logcat for this app is a nullPointer exception for the EchoThread class, at this line:</p>
  
  <pre><code>  handler.post(new Runnable() {
  </code></pre>
  
  <p>why am I getting this error message and what to do about it?</p>
  
  <p>Server class:</p>
  
  <pre><code>  public class ServerThread implements Runnable {
  
      public void run() {
          try {
              if (SERVERIP != null) {
                  handler.post(new Runnable() {
                      @Override
                      public void run() {
                          serverStatus = "Listening on IP: " + SERVERIP;
                      }
                  });
                  serverSocket = new ServerSocket(SERVERPORT);
                  while (true) {
                      // listen for incoming clients
                      Socket client = serverSocket.accept();
                      handler.post(new Runnable() {
                          @Override
                          public void run() {
                              Intent intent = new Intent();
                              intent.setAction("com.example.diceRolled");
                              intent.putExtra("serverStatus","Connected");
                              sendBroadcast(intent);
                          }
                      });
  
                      new EchoThread(Server.this, client).start();
  
                  } // end while(true) method  
  
              } else {  // the rest of the code for Server class....
  </code></pre>
  
  <p>EchoThread class:</p>
  
  <pre><code> public class EchoThread extends Thread {
          protected Socket socket;
          private String line;
          private Handler handler;
          private Context context;
          private Socket client;
  
        public EchoThread(Context c, Socket clientSocket) {
                          this.socket = clientSocket;
                          this.context = c;
        }
  
        public void run(){
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  
            while ((line = in.readLine()) != null) {
            //    Log.d("ServerActivity", line);
                handler.post(new Runnable() {
                    private String receivedCommand;
  
                  @Override
                public void run() {
                receivedCommand = line;
  
                Intent intent = new Intent();
                  intent.setAction("com.example.diceRolled");
                  intent.putExtra("serverStatus", line.trim());
                  context.sendBroadcast(intent);
  
  
    Toast.makeText(context, "sent message " + receivedCommand, Toast.LENGTH_SHORT).show();
  
    // the rest of the code for EchoThread class....
  </code></pre>