-2

I have a code that crashes and gives the following message: java.lang.IndexOutOfBoundsException: Index: -1, Size: 9

What does Index -1 mean?

melpomene
  • 84,125
  • 8
  • 85
  • 148

2 Answers2

0

Somewhere in your code, your index variable is getting a negative value. Could you please provide some code?

kutsyk
  • 185
  • 1
  • 13
  • It is on "Vehicle vehicle = occasionMarket.get(model).remove(index);" that the code crashes. `public static Vehicle buyOccasionCar(String model, int index)` `{` `d_occasionSales++;` `Vehicle vehicle = occasionMarket.get(model).remove(index);` `vehicle.resetPurchaseTick();` `registered_vehicles.put(vehicle.getModel(),` `registered_vehicles.get(vehicle.getModel())+1);` `return vehicle;` } – Baboo335 Apr 28 '17 at 11:35
  • You should check if you index has proper value before making any operation with array or list. So before executing: `occasionMarket.get(model).remove(index);` you should check if 'index >= 0 && index < occasionMarket.get(model).length` – kutsyk Apr 28 '17 at 11:37
  • Thank you for the answer, I am really new at this! How do I check the index? – Baboo335 Apr 28 '17 at 11:38
0

It means that the array/ list etc you are using have total size of 9 items. But you are trying to get a record/item with index "-1". Because in most languages, array/list indexes start from "0" to "size - 1", so you can't use index either smaller than 0, or greater than size of array/list

Afridi
  • 6,753
  • 2
  • 18
  • 27