RaenirSalazar RaenirSalazar - 7 months ago 27
Java Question

When using ".Get(0)" on an empty list, I get an out of bounds exception and not null?

So in my homework assignment, for my error checking testing stuff I do a get on an Object List and I get out of bounds thrown in my face. I solve it by using a check on ".IsEmpty" but what I would like to know why doesn't:

if ( Blah.Blah[blargh][blaaarcgh].MyList.Get(0) != null )



Because when I run the debugging and look at "MyList" I see 9 entries of [ null ]; I can see it as being size 0 though, so that's probably why? It's size 0, so when I try to get an entry it doesn't exist?


If it is Java:

        ArrayList<Object> list=new ArrayList<Object>();
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at HelloWorldTester.main(HelloWorldTester.java:7)

The reason is actually on the source code. rangecheck is probably checking if what you are trying to get is lower than the size of the list. If it is higher then

throw new IndexOutOfBoundsException();