Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:07:49+00:00 2026-06-02T13:07:49+00:00

im getting sometimes null pointer exception while accessing handler created in thread. I`m using

  • 0

im getting sometimes null pointer exception while accessing handler created in thread.

I`m using two approaches.

One is service with thread, in which i create handler and access it by service method.
Second is Thread created in activity, in which i`m making Thread, starting it, and making handler.

Problem is quite simple, handler is created asynchronously to main thread. And i’am accessing it in main (gui thread), so sometimes it will create, and sometimes it will be null at time of getting it.

i could make on gui thread while(handler == null){
}
but it’s really bad solution. I`m looking for some elegant way to do this.

Everything is in main thread.

Thread t = new Thread(new Runnable {
Looper.prepare();
handler = new Handler();
Looper.Loop();
}
handler.post(new Runnable{}) //at this point sometimes handler is still null. 
and it is created like few ms later. But still at this point i need valid handler
to background thread
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:07:50+00:00Added an answer on June 2, 2026 at 1:07 pm
    1. Unless “hander” attribute is volatile, using a while-loop or sleeping is not only unelegant, but also wrong – even if you find that handler is not null, you are not allowed to use it. Since there is no happens-before relation between writing to handler and creating hander instance, the main thread might see the reference to handler BEFORE ITS CONSTRUCTOR IS RUN;

    2. A safe choice to hand off a simple reference is SynchronousQueue (it’s not really a queue, it has no internal capacity, it’s only there to move an object between threads). You would use it like so (off my head, some typos highly possible):

    
        final SynchronousQueue<Handler> giveMeHandler = new SynchronousQueue();
        new Thread(new Runnable(){
           public void run(){
              Handler handler;
              // create a Handler;
              giveMeHandler.put(handler); // blocks until handler taken;
           }
    
        }).start();
    
        Handler handler = giveMeHandler.take(); // blocks until handler given
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MPMediaQuery and then getting the MPMediaitemPropertyAssetURL from the song results sometimes returns null
One of our programs is sometimes getting an OutOfMemory error on one user's machine,
I am getting NullPointerException from the below line sometimes. System.out.println(Date::+ row != null ?
I am getting this exception sometimes when my program is starting connection to MS
I'm getting a JSON string from a WCF service but I'm getting issues sometimes
Hi I'm getting nullpointerexception at rs.next() or rs.getString(1) it is really weird that sometimes
I'am getting sometimes the following error on my published web app with ELMAH: System.Web.HttpException
We are sometimes getting an OutOfMemoryError in production and I would like to be
When I'm posting via ajax I'm sometimes getting extra characters posted for example. If
Sometimes I m getting unicode errors like below in my django site when user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.