Handler thread instance becomes null after creation of the thread

258 views Asked by At

I am getting null pointer exception while quit the thread using its handler thread instance, The instance value becomes null after its creation. If i make the variable as static then I didn't face this problem. Can any one explain me about why the Handler thread instance becomes null after its creation, Below code will explain briefly about my problem.

private class MyHandler extends Handler{

    private HandlerThread hThread;

    private MyHandler(Looper looper){
         super(looper);
    }

    public MyHandler(){

       //Created my handler thread instance here
       hThread = new HandlerThread(... , Process.THREAD_PRIORITY_BACKGROUND);
       hThrad.start();

       new MyHandler(hThread.getLooper);
    }

Here is my handle message method:

  public void HandlerMessage(Message msg){
        super(msg);
     try{


       /*Here I am executing some utility methods*/
     }catch(Exception exce){

     }finally{

         /*While reading the instance of Handler thread it says null*/
            hThread.quit();//Throws null pointer exception.

     }

    }
}
0

There are 0 answers