Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Getting an IndexOutOfBoundsException: index: 1, size: 1 error when trying to get null indexes

Ask Question

Im working on a project where if two objects are of the same class, a new object of that type is inserted into a random null index of an object array of that type. The null indexes should not be repeated.

To get the actual indexes of where the original array has null values(no objects), first I added the null indexes, shuffled them and checked if they are an instanceof an object and if so, a new object of that type is added to the non repeating random null index of the original array. I did this:

List<Integer> nullIndexes = new ArrayList<>();          
for(int i = 0; i < original.length; i++)
   if(original[i] == null)
      nullIndexes.add(i);
Collections.shuffle(nullIndexes);                        
int index = nullIndexs.get(1);                  
if(original[0] instanceof Cat)
   original[index] = new Cat();
if(original[0] instanceof Dog)
   original[index] = new Dog();

But almost every other time I run the program I am getting the below exception and cant figure out why:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at proj2sp16.Proj2App.main(Proj2App.java:437)

Java uses zero based lists. You have to check the size before accessing the element at index one:

   if (nullIndexs.length >1)
       int index = nullIndexs.get(1);

or if you want to Access the first element you have to use

   if (nullIndexs.length >0)
       int index = nullIndexs.get(0);
                The original array has a size from 15-60(randomly chosen). so when it loops, shouldn't the add method add all the indexes that are null in the original array to the nullIndexes array? There is almost always atleast one null index   in the original, so since im trying to access the first index i dont understand where its giving me an issue. Thanks for the quick reply.
– Michael Cera
                Feb 24, 2016 at 7:52

Checking the length of the list before proceeding is always a better way to code.

List<Integer> nullIndexes = new ArrayList<>();
for(int i = 0; i < original.length; i++)
    if(original[i] == null)
    nullIndexes.add(i);
Collections.shuffle(nullIndexes);
if(nullIndexes.size() > 0)
    int index = nullIndexs.get(1);
    //Code here

The exception is self explanatory - Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1.

In java IndexOutOfBoundException occurs when you tried to access an index that is not exist.

ArrayList index starts from 0. So if you want the first result you need to search by 0. You can check the size of an arraylist by arraylist.size() method. You can also check a list whether it is empty or not by arraylist.isEmpty() method

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.