I am having serious difficulties to understand how can I make some AsyncTask children, declared and instantiated in the Main Thread, to await for a Service child instance to reach some specific state.

As code examples here is the relevant part for Service; this code does what expected: receives the JSON response and holds it.

```
public class MyService extends Service {
    private boolean received = false;
    private string url = "http://someserver.mine/get-data-in-json-format";

    // [...]
    @Override
    public void onCreate() {
        doHttpJsonQuery();
    }

    public boolean responseReceived() {
        return this.received;
    }

    public List<MyModel> getResponseAsObject() {
        if (!this.received) return new ArrayList<MyModel>;

        // Many code lines that convert the data into a list.
        // [...]

        return the_list;
    }

    // [...]
    private void doHttpJsonQuery() {

        OkHttpClient client = new OkHttpClient.Builder()
                .build();

        Request request = new Request.Builder()
                .url(url)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                call.cancel();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String myResponse = response.body().string();
                //...and some code to hold data as JSONArray
                //[...]
            }
        });
        this.received = true;
    }
}

```

This Service works; fine. Then, from another class (which purpose will be to handle persistence inserting the received data in a local Room Database), I try to do the following (here's where my mind is blown):

```
public class DataRepository {
    private MyRoomDatabase db;
    private MyModelDao mModelDao;
    // I'm skipping irrelevant code on purpose
    // [...]

    public DataRepository(Application application) {
        db = MyRoomDatabase.getDatabase(application);
        mModelDao = db.myModelDao();
        // [...]

        //  Here I instance a subclass of ContextWrapper(i named it RemoteDataSource) which
        // responsability will be handling different Services for making HTTP operations
        mRemoteDataSource = new RemoteDataSource(application.getApplicationContext());
        // It holds a reference to MyService. It has some public methods, like this one, to
        // control the referenced Service from outside with some encaspsulation
        mRemoteDataSource.startMyService();

        // Instantiating a private nested class...
        PopulateDbAsync mPopulateDbAsync = new PopulateDbAsync(db);
        mPopulateDbAsync.doInBackground();
    }
    // [...]

    // Here is the failing code
    private class PopulateDbAsync extends AsyncTask<Void, Void, Void> {
        PopulateDbAsync(MyRoomDatabase db) {}

        @Override
        protected Void doInBackground(final Void... params) {
            MyService mService = mRemoteDataSource.getMyService();
            if (mService == null) {
                // This doesn't happen at all right now...
                Log.e("MY_ERROR","DataRepository.PopulateDbAsync --> MyService from RemoteDataSource is NULL!!!!");
            }
            List<MyModel> the_list = mService.getResponseAsObject();
            if (the_list == null) {
                // HERE! I obtain the NullReferenceException here.
                // I am confused about how would I avoid this flaw in my code
                Log.e("MY_ERROR", "DataRepository.PopulateDbAsync --> error: response isn't ready yet.");
            }
            for (MyModel i_model : the_list) {
                Log.d("MY_LOG", "DataRepository.PopulateDbAsync --> Inserting data in local DB...");
                mModelDao.insert(i_model);
            }
            return null;
        }
    }
}

```

Summarizing: my problem is that I will always get `NullReferenceException` in this line:

```
for (MyModel i_model : the_list) {

```

I am not familiar with multithreading, asyncronous operations and concurrent execution. I have been reading, for two weeks, lots of different documents on the Internet both from Android Official Website and from other websites as well, trying to figure it out... "AsyncTask is not good to perform this kind of operations"... so, what infrastructure should I implement, I have been wondering... should I use Handlers, Threads, Messengers, or what? The more I read, the more confused I get. It's like I have an [Analysis Paralysis](https://en.wikipedia.org/wiki/Analysis_paralysis) issue...

Most of the examples I find out there provide too verbose code examples on how to implement multithreading and concurrent execution; while I read them and try to figure out how to implement those structures in my code, I just get stuck; also, with so many classes to choose, I get even more confused...

Due to the HTTP call will need to be performed asyncronously (and response time will not always be the same), I am trying to figure out how to make the piece of code that throws the `NullReferenceException` "wait" for MyService to complete it's job before starting it's execution; `while` loops will not work due to it would break Main Thread's lifecycle. "Knowing" if the Service completed it's task or not would be as simple as using the boolean method `responseReceived`. The big idea is, every time new data is obtained through HTTP, updating the RoomDatabase with it, and, meanwhile, MainActivity would be showing the current local data (if any, or an empty list if there's nothing yet).

So, when I get it, I will understand how to refactor the whole code structure properly to start adding more `Service` child instances into my `RemoteDataSource` class, which I created with the idea of having all Service childs that will use `OkHttp` to perform HTTP communications, wrapped together in a single class for better organization.

What would be the proper way to achieve what I am looking for about this? Would someone be able to provide some short example explaining the code structure I will need for something like this? Examples with empty blocks containing comments like "code to execute when ready here" would be great so I can figure it out.

The question exposed here is related with the same project that made me post [this other question](https://stackoverflow.com/q/52202101/3692177) some weeks ago; I have been reading here and there, performing some trial-and-error and correcting some code issues here-and-there since then; however, I am making a different question here; finding an answer for this would probably be the first step towards figuring out an answer to the other question as well.

URL References to documentation I have been reading
---------------------------------------------------

Some of the documentation I have been reading (but not limited to):

* [AsyncTask class documentation](https://developer.android.com/reference/android/os/AsyncTask)
* [Handler class documentation](https://developer.android.com/reference/android/os/Handler)
* [Basics on Multithreading](https://developer.samsung.com/galaxy/others/multithreading-in-android)
* [Introduction to background processing in Android - Tutorial](http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html)
* [Thread With Handlers - Android Example](https://androidexample.com/Thread_With_Handlers_-_Android_Example/index.php?view=article_discription&aid=58&aaid=83)
* [Messenger class documentation](https://developer.android.com/reference/android/os/Messenger)