Paulo wrote:when you try access a null index in a List object
I would say "when you try to access a list of objects with an index out of the range of the list".
Paulo wrote:Why it don't throws null, like the Map Collection, for example?
Usual Maps are not bounded, you don't use index to access them, but keys. A key is not ouf of range. It exists, or it doesn't. Some custom maps, like org.apache.commons.collections.map.LinkedMap, use indexes to access the values. IndexOutOfBoundsException will also be thrown if the index used is out of range.
This is not an index, this is a key. In a Map, you associate some values to some keys, and retrieve the values via the keys. In your example, you are using Integers for keys. These are not indexes, these are still keys.
1 -> "One"
2 -> "Two"
map.get(2) : "Two"
map.get(3) : null
IndexOutOfBoundsException will never be thrown, because this is not an index you are using. The value associated to 3 does not exist, so null is returned.
List are different. You can access elements via an index. This index ranges from 0 to the size of the list(excluded). If the index is out of this range, IndexOutOfBoundsException will be thrown.
Joined: Jun 01, 2010
EDIT: I posted this at the same time as Christophe Verré so this is probably redundant.
Lists are all about indexes. Maps are all about key-value pairs.
As Christophe Verré suggests, I think you are confusing indexes with keys. In Lists, you can have multiple null values. You also always know when an index is valid (0 to List.length - 1) so trying to access anything outside those indexes is considered a programming error. For maps, you don't always know the keys so asking for the corresponding value of a key is not considered a programming error and therefore returns null if the key does not exists in the map.