0

I have an arraylist inside an arraylist (nested arraylist) as below

  ArrayList<ArrayList<Integer>> indexOfJSONObject = new ArrayList<ArrayList<Integer>>();

Now I need to get an instance of the arraylist that exists in a given index of the indexOfJSONObject arraylist and add a value to it. I used the following code

    ArrayList<Integer> tempJSONObjectAL= (ArrayList<Integer>)indexOfJSONObject.get(givenIndex);

    tempJSONObjectAL.add(value); 

but it gives me the error of

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

how to fix this and why this happening.

Thank you

chancyWu
  • 14,073
  • 11
  • 62
  • 81
Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58
  • It's not very hard to unserstand the error : You want the element at the index 0 , so the first BUT the list is empty : size = 0 You have apparently not provide any data to indexOfJSONObject – azro Feb 14 '17 at 09:41
  • 1
    it's happeninng because `indexOfJSONObject` does not contain any items and per doc spec calling `get` on empty list results in `IndexOutOfBoundsException` – marknorkin Feb 14 '17 at 09:41
  • indexOfJSONObject is null at the time you are calling it – Akshay Feb 14 '17 at 09:42
  • I guess you basically do not have anything within the first location of the arrayList :-) That's basically the reason your'e getting the error IOOBE. As adding an empty arraylist to an arraylist does not make any sense - as the first element will be an empty object :-) However, if you new ArrayList<> first, then add a new ArrayList<> after that, things will work as intended :-). – vegaasen Feb 14 '17 at 09:43
  • 1
    also maybe it would be more convinient and readable to use `Map>` – marknorkin Feb 14 '17 at 09:44
  • You can first use ArrayList tempJSONObjectAL= (ArrayList)indexOfJSONObject.getList();..Then get the first object – Akshay Feb 14 '17 at 09:44
  • @Akshay it say that `getList()` undefined for array list – Kasun Siyambalapitiya Feb 14 '17 at 09:51
  • Ok...I got it...see my answer – Akshay Feb 14 '17 at 09:54

3 Answers3

2

The problem here seems that list size is 0 and you are still trying to access an element at position 0.

You should never try to directly access an element in a collection, without using a loop/iterator or without putting a check comparing size and givenIndex. Your code should be something like

ArrayList<Integer> tempJSONObjectAL= null;
if(indexOfJSONObject.size() > givenIndex)
    tempJSONObjectAL = (ArrayList<Integer>)indexOfJSONObject.get(givenIndex);
Nipun
  • 65
  • 9
2

ReasonThis is very simple. This error is because indexOfJSONObject is an ArrayList which itself holds an ArrayList. But You haven't any ArrayList inside the indexOfJSONObject.
You are initially getting the ArrayList from indexOfJSONObject, while there is no ArrayList Instantiation inside it.
You need to add a new Instantiation of ArrayList to the indexOfJSONObject and then work with it.

By adding one specific statement will solve the issue. Just check my code below:

ArrayList<ArrayList<Integer>> indexOfJSONObject = new ArrayList<ArrayList<Integer>>();

//This line of code is required in your case
indexOfJSONObject.add(new ArrayList<Integer>());

ArrayList<Integer> tempJSONObjectAL= (ArrayList<Integer>)indexOfJSONObject.get(givenIndex);

tempJSONObjectAL.add(value); 
Tahir Hussain Mir
  • 2,506
  • 2
  • 19
  • 26
1

Try the following code:

ArrayList<ArrayList<Integer>> indexOfJSONObject = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> tempJSONObjectAL=new ArrayList<Integer>();

        for(ArrayList<Integer> list:indexOfJSONObject)
        {
            tempJSONObjectAL.add(list.get(index));

        }
Akshay
  • 1,161
  • 1
  • 12
  • 33