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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:47:12+00:00 2026-06-19T04:47:12+00:00

I have an object, MyObject , that populates itself with some random numbers. It

  • 0

I have an object, MyObject, that populates itself with some random numbers. It doesn’t take very long and is seemingly instant. However, these values are used on the UI and on occasion it isn’t completely ready when the UI is building.

I’ve created an AsyncTask that is used to instantiate the object and it’s at the end of this post.

However doInBackground(...) doesn’t instantiate the object completely before onPostExecute(...) is called when it is in this fashion.

protected MyObject doInBackground(Void... unused) {
    // this may take a few moments and cause a null reference
    // so make a task
    MyObject obj = new MyObject();
    return obj;
}

I’ve also tried to use a flag to signify completion, but it doesn’t appear to guarantee either.

MyObject obj = new MyObject();
while (obj.isComplete() == false) {
    // do nothing. Wait for completion
}

return obj;

Inside of MyObject is simple

public MyObject() {
    createValues(); // entire object created with this
    this.mComplete = true;
}

It fails less, but still does. createValues() uses an ArrayList for part of its calculations if that is contributing to the race condition.

Below is an example MyFragment that uses the task in methodCall().

Question How do I simply wait until MyObject is completely instantiated to end?

Stacktrace

java.lang.NullPointerException
    at MyFragment.setupFragment(MyFragment.java:95)
    at MyFragment.onTaskComplete(MyFragment.java:143)
    at MyTask.onPostExecute(MyTask.java:37)
    at MyTask.onPostExecute(MyTask.java:1)
    at android.os.AsyncTask.finish(AsyncTask.java:631)

Fragment

public class MyFragment extends Fragment implements MyTask.OnTaskComplete {
    private MyObject mMyObject;

    // ...

    public void methodCall() {
        // generate puzzle and set it up in the background
        MyTask task = new MyTask(this);
        task.execute();
    }

    function void setupFragment() {
            // null pointer exception here
            // mMyObject is not initiated yet and only some times
            // values is an array of integers
        int example = this.mMyObject.values[0];
    }

    @Override
    public void onTaskComplete(MyObject obj) {
        // returned from MyTask
        this.mMyObject = obj;
        this.setupFragment(); 
    }
}

AsyncTask

public class MyObjectTask extends AsyncTask<Void, Void, MyObject> {
    public static interface OnTaskComplete {
        public abstract void onTaskComplete(MyObject obj);
    }
    static final String TAG = "MyObjectTask";

    private OnTaskComplete mListener;

    public MyObjectTask(OnTaskComplete listener) {
        this.mListener = listener;
    }

    @Override
    protected MyObject doInBackground(Void... unused) {
        // this may take a few moments and cause a null reference
        // so make a task
        return new MyObject();
    }

    @Override
    protected void onPostExecute(MyObject obj) {
        this.mListener.onTaskComplete(obj);
    }
}
  • 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-19T04:47:13+00:00Added an answer on June 19, 2026 at 4:47 am

    What you are doing should be completely fine if you are not creating any new threads inside your object when creating it.

    You have added a callback which you need to fist set before executing the task and then it will be triggered when the AsyncTask exits (doInBackground finishes and your post execute is called).

    So from what i am seeing in your code there is no way the constructor doesn’t finish and your object is returned in the callback.

    So check if possibly something throws an Exception in your object in the method createValues.

    Do you have a stacktrace of the exception that happens?

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

Sidebar

Related Questions

I have an object model MyObject that contains a list of long called ObjectList
In C#, suppose you have an object (say, myObject ) that is an instance
I have an object that looks like this: public class MyObject { public int
Let's say I have an object MyObject that looks like this: public class MyObject
I have an object model called MyObject that I'm serializing to a json string
I have a class that represents some business object, such as this: Public Class
If I have an object MyObject that contains an array of objects ListOfOtherObjects and
I have an object MyObject and I'm storing that in the session. I write
I have an NSMutableArray, that should be only modified by the owner object (MyObject).
I have an object MyObject that contains a property MyProp that is sometimes numeric.

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.