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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:08:48+00:00 2026-06-18T12:08:48+00:00

I have an app that is loading data from text files and storing the

  • 0

I have an app that is loading data from text files and storing the data from each file into a separate ArrayList. I was told to load the data on an async thread even though my app does not crash loading it all from the onCreate method (it just takes approx 12 seconds to load all of the data from the text files into the ArrayLists).

I have attempted to set up an async thread, but I have run into an issue. I am brand new to async threads, so I do not know/yet fully understand all of the details (I am trying to piece info together from different sources). Here is the relevent code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name);

    new loadData().execute();

    arrayListElement = ArrayList1.get(0);

}

private class loadData extends AsyncTask<Void, Void, Void> {

    ProgressDialog dialog;
    protected void onPreExecute() {
        dialog = new ProgressDialog(ClassName.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub
        populateArrayLists();
        return null;
    }

    /*protected void onProgressUpdate() {

    }

    protected void onPostExecute() {

    }*/

}

I am getting an IndexOutOfBoundsException (invalid index 0, size is 0) at the last statement in the onCreate method.

All I want to do is load the data on a separate thread because even though my app does not crash, I was told I should do it. The UI thread should handle all of the rest. The app was working fine until I added the async thread code.

How my files are read can be seen here:
Android: Asynch Thread Needed?

What am I doing wrong?

  • 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-18T12:08:49+00:00Added an answer on June 18, 2026 at 12:08 pm

    You are returning null from doInBackground, so of course this all fails. Use the AsyncTask for what it’s meant to do. To get it to work correctly, you need to correctly define your AsyncTask with the appropriate type paramaters.

    Instead of making populateArrayLists have side effects of updating some other field, have a method like:

    void List<Foo> getListOfFoo() {
      ArrayList<Foo> listOfFoos = getTheListSomehow();
      return listOfFoos;
    }
    

    For example:

        public class MyActivity extends Activity {
    
    
            public void doSomething(List<String> stringList) {
               //do something here
    
            }
    
            class MyAsyncTask extends AsyncTask<Void, Void, List<String>> {
    
                @Override
                protected List<String> doInBackground(Void... params) {
                    ArrayList<String> listOfStrings = getListOfStrings();
                    return listOfStrings;
                }
    
                @Override
                protected void onPostExecute(List<String> strings) {
                    super.onPostExecute(strings);
                    doSomething(strings);
                }
    
                private List<String> getListOfStrings() {
    ArrayList<String> stringList = new ArrayList<String>();
                    //This is where you'd actually perform your expensive operation
      BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(getAssets().open(
                    "text1.txt")));
            String text;
            while ((word = br.readLine()) != null) {
                stringList.add(text);
            }                           
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close(); // stop reading
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
                    return stringList;
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have application reads in data from text file. Recently I realized that I
I am creating an app that involves reading data from text files that are
I have an app that received JSON data from the server. Currently when I
I have an app that allows users to upload large data files and then
I have a model that requires loading external data from an auxiliary source. A
I have a Silverlight Windows Phone 7 app that pulls data from a public
I'm writing a web-app for the iPad that will be loading data from a
I have a simple app that downloads some data from a webservice and displays
I have an error where I am loading data from a web-service into the
I have a little iPhone app that loads data from a web service. To

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.