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 6900831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:35:48+00:00 2026-05-27T07:35:48+00:00

I was just trying to figure out if I could get a NULL pointer

  • 0

I was just trying to figure out if I could get a NULL pointer exception with the following code. The cause could be this: The check is done at time X. But, I post the runnable to the handler, he will execute at X+5. He should have a strong reference, therefore preventing the Runnable being gc-ed in between.

Am I correct? (that what I call easy reputation, a YES is enough. A no, you have to explain 🙂

public class WeakRunnableUiList
{
    private ArrayList<WeakReference<Runnable>> _items = new ArrayList<WeakReference<Runnable>>();
    private Handler _handler = new Handler(Looper.getMainLooper());

    public void Add(Runnable r)
    {
        _items.add(new WeakReference<Runnable>(r));
    }

    public void Execute()
    {
        ArrayList<WeakReference<Runnable>> remove = new ArrayList<WeakReference<Runnable>>();
        for (WeakReference<Runnable> item : _items)
        {
            if (item.get() == null)
            {
                remove.add(item);
            }
            else
            {
                _handler.post(item.get());
            }
        }
        _items.removeAll(remove);
    }
}
  • 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-05-27T07:35:48+00:00Added an answer on May 27, 2026 at 7:35 am

    He should have a strong reference, therefore preventing the Runnable
    being gc-ed in between

    No.
    Put this into your code just before _handler.post(...:

    ...
    byte[] b=new byte[1024*32]; // this can occur in an other thread!
    if (item.get() == null) {
        Log.e("Item is NULL now!", "Item is NULL now!");
    }
    _handler.post(item.get());
    ...
    

    And then in the main program:

    for(int i=0;i<100;i++) {
        weakRunnableUiList.Add(new X());
    }
    weakRunnableUiList.Execute();
    

    Will give you 12-03 21:56:01.521: E/Item is NULL now!(1071): Item is NULL now!

    So the runnable can get NULL after your first check!

    Do it like this:

    Runnable r=item.get();
    if (r==null) ...
    

    But you can post nulls to handlers: _handler.post(null);, and it will not throw a nullpointer actually.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out just how a particular function works on a Facebook
I just went through a bunch of stuff trying to figure out how to
I'm just learning asp.net mvc and I'm trying to figure out how to move
Just trying to get my head around Generics by reading this enlightening article by
Just trying to get my head round Spring and figuring out how I wire
just trying to test for equality in this piece of code, but getting a
I've spent the last 2 days trying to figure this out, basically I have
After two days of bashing my head and trying to figure this stuff out
I'm trying to figure out how to get the X and Y coords of
Just trying to get my irb sessions to actually list the current line of

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.