Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did I get an "ArrayIndexOutOfBoundsException"?

Tags:

java

arrays

I'm pretty new to programming and working on an assignment for class. Now, I'm not asking for anyone to write my code for me but I'm stuck with a runtime error. In the assignment we need to read a file, use the first line, "15", to initialize the size of an array, and proceed to fill the array with the information from each line.

edit: I didn't want to post all of the code because I thought it would look too long but because of the downvotes for being vague, here it goes.

File:

15
produce,3554,broccoli,5.99,1
produce,3554,broccoli,5.99,1
produce,3555,carrots,2.23,0.25
produce,3555,carrots,2.23,0.25
produce,3555,carrots,2.23,0.25
cleaning,2345,windex,5.99,1 unit
cleaning,2345,windex,5.99,1 unit
cleaning,2345,windex,5.99,1 unit
cleaning,2345,windex,5.99,1 unit
cleaning,2346,toilet paper,12.99,4 rolls
cleaning,2346,toilet paper,12.99,4 rolls
cleaning,2335,windex,2.25,1 mini sprayer
cleaning,1342,wipes,3.99,10 units
cleaning,1342,wipes,3.99,10 units
produce,3546,lettuce,2.99,0.5

My Error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
    at Inventory.readFile(Inventory.java:45)
    at Inventory.<init>(Inventory.java:12)
    at Supermarket.main(Supermarket.java:3)

Class with the Line 45 in Question (line 45 is commented, scroll to the right)"

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class Inventory{
    Product[] list;
    String[] invData;
    private int i = 0;
    public int count;

    public Inventory (String f){
        readFile(f);
    }

    public int indexOfProduct(int code){        
        for(i=0; i<list.length; i++){ 
            if (list[i] != null)
                if (list[i].getCode() == code)
                    return i;

        }
        return -1;
    }


    public Product delete(int pos){
        Product temp = new Product();
        temp = list[pos];
        list[pos] = null;
        return temp;
    }

    public void readFile(String fileName){
        try{
            File invList = new File (fileName);
            Scanner s = new Scanner(invList);
            int itemCount = s.nextInt();
            list = new Product[itemCount];
            count = itemCount;
            while (s.hasNext()){
                String line = s.nextLine();
                invData = line.split(",");
                if (invData[0].equals("produce")){
                    list[i] = new Produce(invData[1], invData[2], invData[3], invData[4]); // This is Line 45, Where the error occurs
                } else if(invData[0].equals("cleaning")){
                    list[i] = new Cleaning(invData[1], invData[2], invData[3], invData[4]);
                }
                i++;
            }//end of while loop
        } catch (FileNotFoundException Abra) {
            String error = Abra.getMessage();
            System.out.println(error);
            } 
    } // end of method

    public Product findCode(int c){
        for(int i=0; i<list.length;i++)
            if(list[1].getCode() == c)
                return list[i];
        return null;
    }//end of method
}//end of class

Why did I get an "ArrayIndexOutOfBoundsException"? I hope someone can point out the flaw in my logic so I don't repeat it again.

like image 877
Jeffrey Paz Avatar asked Aug 06 '13 02:08

Jeffrey Paz


People also ask

Why do I get ArrayIndexOutOfBoundsException?

What Causes ArrayIndexOutOfBoundsException. The ArrayIndexOutOfBoundsException is one of the most common errors in Java. It occurs when a program attempts to access an invalid index in an array i.e. an index that is less than 0, or equal to or greater than the length of the array.

What does ArrayIndexOutOfBoundsException mean?

ArrayIndexOutOfBoundsException occurs when we access an array, or a Collection, that is backed by an array with an invalid index. This means that the index is either less than zero or greater than or equal to the size of the array.

At which point of execution does the ArrayIndexOutOfBoundsException occur?

ArrayIndexOutOfBoundsException is a runtime exception and thrown only at the execution state of the program.

How do I fix IndexOutOfRangeException?

Solutions to Prevent IndexOutOfRangeException Solution 1: Get the total number of elements in a collection and then check the upper bound of a collection is one less than its number of elements. Solution 2: Use the try catch blocks to catche the IndexOutOfRangeException .


1 Answers

Your issue is clearly with the use of i, as that is the only variable index on that line, and the out of range index is "15", which is just past the end of your 15-item array. So, couple of issues, all surrounding the use of i:

As nhellwig mentioned, be sure that i is actually initialized to 0 before calling this function.

Additionally, you're putting a lot of faith in the consistency of the item number in the file and the actual number of items. You should either produce a warning and stop trying to store items in the array if i >= itemCount, or use a container like an ArrayList that can grow to accommodate new items instead of a fixed size array.

Edit: Also, I should point out that you increment i whether you read an item or not, which means even blank lines will increment i, causing gaps in your list or array overruns. Since itemCount is the number if items, you should stick to that and only increment i if you read an actual item.

In that same spirit, you should verify that invData.length == 5 after you call split(), because a misplaced comma, etc. in your file may also end up with an OOB error. Granted, for your project, it's probably OK to make assumptions about the number of elements in a line that starts with "produce" or "cleaning", but in general it's important to be cautious with data coming from a user-created file.

like image 63
Jason C Avatar answered Nov 16 '22 00:11

Jason C