Why did I get an "ArrayIndexOutOfBoundsException"?

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

edit: I didnโ€™t want to publish all the code because I thought it would look too long, but due to the fact that downvotes will be vague, here it is.

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 mistake:

 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 line 45 in question (line 45 commented, scroll 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 a flaw in my logic, so I do not repeat it again.

+7
java arrays
source share
4 answers

Your problem is clearly using i , since this is the only variable index on this line, and the index outside the range is "15", which is located a bit later than the end of your array of 15 elements. So, a couple of questions related to using i :

As mentioned above, make sure that i actually initialized to 0 before calling this function.

In addition, you place great faith in the consistency of the position number in the file and the actual number of elements. You should either issue a warning, or stop trying to store items in an array if i >= itemCount , or use a container of type ArrayList, which can grow to accommodate new items instead of a fixed-size array.

Edit: In addition, I must indicate that you increment i , whether you read the element or not, which means that even empty lines will increment i , causing spaces in the overflow of the list or array. Since itemCount is a number if there are items, you should stick to this and only increment i if you are reading the actual item.

In the same vein, you should check that invData.length == 5 after calling split (), because an inappropriate comma, etc. in your file may also result in an OOB error. Of course, it is probably good for your project to make assumptions about the number of elements in a line that starts with โ€œproduceโ€ or โ€œclearโ€, but in general it is important to be careful with the data coming from a user-created file.

+6
source share

I found the answer that I need "s.nextLine ();"

Because I used "s.nextInt ();" the pointer was just hanging at the end of the "15" in my file. Then, when the first line in the While loop is "Line String = s.nextLine ();" executed a pointer moved from the end of 15 to the file p in the second line of the list file.

The working method is as follows:

 public void readFile(String fileName){ try{ File invList = new File (fileName); Scanner s = new Scanner(invList); int itemCount = s.nextInt(); s.nextLine(); // This is the new line that made it work list = new Product[itemCount]; count = itemCount; while (s.hasNext()){ String line = s.nextLine(); //moves file pointer over one invData = line.split(","); if (invData[0].equals("produce")){ list[i] = new Produce(invData[1], invData[2], invData[3], invData[4]); } 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 
+5
source share

How many times do you call readFile? You must have i = 0; at the beginning of the function.

+3
source share

"i" should not be a global value, but it must be a local method variable initialized to zero.

+3
source share

All Articles