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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:41:54+00:00 2026-05-30T12:41:54+00:00

It’s my first question, but I’ll try to ask it well. Anyway, I’m trying

  • 0

It’s my first question, but I’ll try to ask it well. Anyway, I’m trying to read data from a text file in Android, but for some reason it keeps returning nothing – namely, IndexOutOfBounds exception. All relevant code:

public Question getNextQuestion(int num) {
    Log.d(TAG, array.toString());
    return array.get(num+1);
}
public ArrayList<Question> getData(String str) throws NumberFormatException, IOException {
    Log.d(TAG, "STARTING getData");
    Log.d(TAG, str);
    ArrayList<Question> a = new ArrayList<Question>();
    String[] strline = str.split("//");
    for (int j = 0; j < strline.length; j++) {
        if (strline[j] == null) {
            strline[j] = "TESTING";
            Log.d(TAG, strline[j]);
        }
    }

    int num = 0;
    int x = 0;
    String y = strline[x];
    while (x == 0 || y != null) {
        num++;
        String[] data = y.split("//");
        Log.d(TAG, y);
        if (data.length >= 7) {
            a.add(new Question(Integer.parseInt(data[0]), data[1], data[2], data[3], data[4], data[5], Integer.parseInt(data[6])));
            Log.d(TAG, a.get(a.size()).getText());
        }
        else if (data.length >= 3) {
            a.add(new Question(Integer.parseInt(data[0]), data[1], data[2]));
            Log.d(TAG, a.get(a.size()).getText());
        }
        x++;
        if (x < strline.length)
            y = strline[x];
        else
            break;
    }
    for (int i=0; i<a.size(); i++){
        Log.d(TAG, a.get(i).getText());
    }
    Log.d(TAG, "ENDING getdata");
    return a;
}
public ArrayList<Question> openFile() throws IOException {
    Log.d(TAG, "STARTING openFile()");
    //FileReader fr = new FileReader("data/data/scibowl.prog/questionsb.txt");
    FileInputStream fis;
    String storedString = "";
    try {
        fis = openFileInput("questionsb.txt");
        DataInputStream dataIO = new DataInputStream(fis);
        String strLine = null;

        while ((strLine = dataIO.readLine()) != null) {
            storedString = storedString + strLine;
        }

        dataIO.close();
        fis.close();
    }
    catch  (Exception e) {  
    }

    array = getData(storedString);
    Log.d(TAG, "DID open file, ending openFile()");
    return array;
}

This will print in LogCat:

    onCreate done
STARTING openFile()
STARTING getData
ENDING getdata
DID open file, ending openFile()
right before Toast.makeText
[]
Caused by: java.lang.IndexOutOfBoundsException: Invalid location 1, size is 0
at java.util.ArrayList.get(ArrayList.java:341)
at scibowl.prog.Round.getNextQuestion(Round.java:55)
at scibowl.prog.RoundView.<init>(RoundView.java:26)
at scibowl.prog.Round.onCreate(Round.java:35)

I’ve tried practically everything from StringBuffers to FileInputStreams to BufferedReaders, and read the related Android documentation, though feel free to tell me I’m not reading it hard enough. Does anybody know why I keep getting arrays with no elements?

Text file for reference, albeit slightly edited to conform with the blockquote rules:

1//On the standard electromagnetic spectrum, which frequency is between gamma rays and ultraviolet rays?//Radio waves//Microwaves//X-rays//Infrared waves//1

2//Which waves on the electromagnetic spectrum roughly encompasses frequencies of 10^9 Hz to 10^12 Hz?//Visible Light//Gamma Rays//Ultraviolet Rays//Microwaves//3

3//Approximately what percentage of the US’s power comes from coal?//30%//40%//50%//60%//0

4//If a 100-kilogram person starts walking from rest and accelerates to 4.0 m/s over 2 seconds at constant acceleration, how much work does he do?//600 J//800 J//1200 J//2000 J//1

  • 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-30T12:41:55+00:00Added an answer on May 30, 2026 at 12:41 pm

    Your logcat suggests it is crashing on line 55 in the method getNextQuestion, which I believe is:

    return array.get(num+1);
    

    Also, your logcat suggests that you’re trying to access a value in the array that doesn’t exist. Meaning you’re trying to retrieve the item at index 1 (the second item, I believe), but the size of your array is 0, so there are no items that you can get.

    So chances are that in getData you’re failing to actually return an ArrayList like you intend. Or the ArrayList you return is empty.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.