Meatuchu
Meatuchu

Reputation: 33

Erroneous Out of Bounds Exception

So, I've retreived a line from a text file and stored it as one string in an array of strings and I've called it inventorylist[i]. I then split it using .split(" ") and store the tokens from that in array called invlistTokens. When I do anything with that token array, it throws an out of bounds exception. If I put it in a forloop to display the 5 tokens I expect, it will succeed in reading them and THEN throw that exception.

    public static item[] loadInv(){
    String inventoryname = "Henderson_j_inv.txt";
    String[] inventorylist= new String[50]; //more than enough room for the file to load in
    String[] invlistTokens = new String [5];
    item[] inventory = new item[50];

    try {
        ReadFile file = new ReadFile(inventoryname);
        inventorylist = file.OpenFile();
    } catch (IOException e) {
        System.out.print("FILE FAILED TO LOAD");
    }

    for(int i=0; i< Array.getLength(inventorylist); i++){
        System.out.println(inventorylist[i]);//This always succeeds
        invlistTokens=inventorylist[i].split(" ");

        for(int j=0; j<5; j++){  //This is the weird ForLoop. It completes and then java throws out of bounds.
            System.out.println(invlistTokens[j]);
        }
    }

Please excuse my messy post, this is my first post and I'm not sure how specific I can be about such a weird error

Eclipse Screenshot: https://i.sstatic.net/HYals.jpg

Eclipse Screenshot

I did get the variable right in my actual code, just happened to be a dummy when adding the forloop to the post

This is the actual exception, except I know that it makes it past 1. The loop completes all 5 runs before it throws.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at shopping2.loadInv(shopping2.java:50)
    at shopping2.main(shopping2.java:23)

So I changed the code to this

public static item[] loadInv(){
    String inventoryname = "Henderson_j_inv.txt";
    String[] inventorylist= new String[50];
    String[] invlistTokens = new String [100];
    item[] inventory = new item[50];

    try {
        ReadFile file = new ReadFile(inventoryname);
        inventorylist = file.OpenFile();
    } catch (IOException e) {
        System.out.print("FILE FAILED TO LOAD");
    }

    System.out.print(inventorylist.length); //This displays 19, when it should be giving me 10 based on my file

    for(int i=0; i< inventorylist.length; i++){
        //System.out.println(inventorylist[i]);
        invlistTokens=inventorylist[i].split(" ");
        System.out.println(invlistTokens.length); //This alternates between 5 and 1


    }

So I think the problem is either in my txt file or my reader class. Thanks for the help, everyone.

Upvotes: 2

Views: 738

Answers (2)

Benjamin
Benjamin

Reputation: 1846

We see two \n before the stack trace, System.out.println() seems to be called twice with an empty String.

So it seems that on the second iteration of i, inventorylist[i] is an empty String. So .split(" ") returns a single-element array with an empty String at index 0.

That's why on the second iteration of j, you have an ArrayIndexOutOfBoundsException at index 1.

Upvotes: 1

Seelenvirtuose
Seelenvirtuose

Reputation: 20618

Your text source contains two lines!

The first line is the one from your screenshot:

01 MusicCD 100 20 5.00

The second one is a simple empty line.

So your little program loops correctly over the first line, printing out the correct results. Then it loops over the second (empty) line and does the following:

  1. Print out the empty line. Look at your screenshot. It contains this written empty line.
  2. Split this empty string by space, resulting in a 1-element-array.
  3. Looping over this 1-element-array, printing out correctly the first element (another empty line -> look at screenshot).
  4. Correctly throwing an AIOOBE for index 1 because there is no such index.

Conclusion: Check for empty lines before going into the loop. Or check the number of elements in the split array.

Upvotes: 1

Related Questions