title: Looper Handler Example
body: <p>I was trying to understand the looper and handler in android, but got stuck with the example written.
  What I am trying to do is, add a looper to the thread, to make thread running continuously in run() method. Then post messages or runnables to the hanlder thread.</p>
  
  <pre><code>public class HLClass extends Thread {
          Handler mHandler;
          @Override
          public void run() {
              super.run();
              Looper.prepare();
              mHandler = new Handler(){
                  @Override
                  public void handleMessage(Message msg) {
                      super.handleMessage(msg);
                      Log.d("HLClass","In Handler, Msg = "+msg.arg1);
                  }
              };
              Looper.loop();
          }
      }
  </code></pre>
  
  <p>This is how I am trying to call handler:</p>
  
  <pre><code>HLClass hlc = new HLClass();
            hlc.start();
            Message m = hlc.mHandler.obtainMessage();
            m.arg1 = 10;
            hlc.mHandler.sendMessage(m);
  </code></pre>
  
  <p>Error:</p>
  
  <p>Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Message android.os.Handler.obtainMessage()' on a null object reference</p>
  
  <p>Here, what really I am trying to understand that, how I can attach a handler to thread and then post message from any other thread
  How can I safely post Message or Runnable to handler without error.</p>