11

I am getting the error java.lang.IndexOutOfBoundsException: Index: 0, Size: 0.

public Collection<AdDistribution> getAdDistribution(byte srch, byte cont) throws IndexOutOfBoundsException {

    List<AdDistribution> mediums = new ArrayList<>();
    List<AdDistribution> adDistribution = new ArrayList<>();
            adDistribution.add(AdDistribution.SEARCH);
            adDistribution.add(AdDistribution.CONTENT);
            if (adDistribution.isEmpty()) {
                return null;
              }

    if (srch == 0 && cont == 0) {
        mediums = new ArrayList<>();
        mediums.set(0, adDistribution.get(0));
    }
    if (srch == 1 || cont == 1) {
        mediums = new ArrayList<>();
        if (srch == 1) {
            mediums.set(0, adDistribution.get(0));
        } else if (cont == 1) {
            mediums.set(0, adDistribution.get(1));
        }
    }
    if (srch == 1 && cont == 1) {
        mediums = new ArrayList<>();
        mediums.set(0, adDistribution.get(0));
        mediums.set(1, adDistribution.get(1));
    }
            return mediums;
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Manju
  • 199
  • 1
  • 2
  • 10

3 Answers3

16

The problem is that you are using set method to update element at index 0

set(index,value) method needs an element to present at that index

but you haven't added any element at that position in medium arraylist before that .

So you need to first add an element at index 0 thereafter only you can update it with set method

Devansh Kumar
  • 1,552
  • 1
  • 15
  • 27
6

You need to use

mediums.add(adDistribution.get(0));

rather than mediums.set(0, adDistribution.get(0)); etc.

ArrayList.set(int, Object) requires there to be an element to replace:

Replaces the element at the specified position in this list with the specified element.

Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

In a new list, size() == 0, so set(0, something) fails.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
2

EDIT: I just removed the unnecessary if statements and simplified them to make your code more readable.
Then I have replaced the set functions with the add function, because the set only can set existing elements.

EDIT2: Based on Andy Turner's answer, I modified my code. Thank you for the suggestions!

After these modifications, the code should look like this (add/remove/modify it for your needs):

public Collection<AdDistribution> getAdDistribution(byte srch, byte cont) throws IndexOutOfBoundsException {

    List<AdDistribution> mediums = new ArrayList<>();

    if( ( srch == 0 && cont == 0 ) || srch == 1 ) {
        mediums.add(AdDistribution.SEARCH);
    }

    if( cont == 1 ) {
        mediums.add(AdDistribution.CONTENT);
    }

    return mediums;
}
  • 1
    "I just removed the unnecessary if statements" you could also remove `if (adDistribution.isEmpty()) {`, which is never false. `adDistribution` is also redundant: just add `AdDistribution.SEARCH` and `AdDistribution.CONTENT` directly to `mediums`. – Andy Turner Aug 22 '16 at 11:44
  • Thanks for the suggestions. At first I did not pay attention to those parts of code, I should have been more accurate about everything. I have edited my comment. Regards! – Pohkalopokh Aug 22 '16 at 12:38