
An array index out of bounds exception is a bug class that occurs in Java when a program tries to access an array element that does not exist. This bug can cause a Java program to crash or behave unexpectedly, and it can also lead to security vulnerabilities. To fix an array index out of bounds exception in Java, you should always ensure that you are accessing array elements within the bounds of the array.

// If you want to factorize the description uncomment the following line and create the file.
//include::../description.adoc[]

== Why is this an issue?

Array index out of bounds exception is the indication of a bug or a logical error in the code. It occurs when you try to access an array element with an index that is outside the valid range of the array. When it happens, it can cause the program to crash or put it into an inconsistent state. This can lead to serious consequences such as data loss. When the program is used for critical tasks such as financial transactions or medical records, the consequences could even be more severe. Therefore, it is important to fix array index out of bounds exception in Java to ensure that programs are reliable, secure, and function as intended.
//=== What is the potential impact?

== How to fix it
//== How to fix it in FRAMEWORK NAME

To fix array index out of bounds exception in Java, you should always ensure that you are accessing array elements within the bounds of the array. Here are some best practices to follow:


Always check the length of the array before accessing elements. This can be done using the `length` property of the array. For example, `myArray.length` returns the length of the `myArray` array. Also ensure that you are not accessing an array element located before position zero. This will ensure that you do not try to access elements that do not exist.

Use loops to iterate over arrays instead of accessing elements directly. This can be done using the `for` loop or the `foreach` loop. For example, `for (int i = 0; i < myArray.length; i++)` iterates over the `myArray` array using an index variable `i`. The same goes for `for (Object o : myArray)`. If, inside the loop, you manipulate the index used to access the array, then make sure that the resulting index stays within the array bound. This will ensure that you do not accidentally access elements that do not exist.


=== Code examples

==== Noncompliant code example

[source,text,diff-id=1,diff-type=noncompliant]
----
  void nonCompliant() {
    int[] l = {1, 2, 3};

    int x = l[-1]; // Noncompliant
    int y = l[3]; // Noncompliant
  }
----

==== Compliant solution

[source,text,diff-id=1,diff-type=compliant]
----
  void compliant() {
    int[] l = {1, 2, 3};

    int x = l[0];
    int y = l[2];
  }
----

== Resources

=== Documentation

* Java Documentation - https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ArrayIndexOutOfBoundsException.html[ArrayIndexOutOfBoundException]
* A Reference Guide - https://www.baeldung.com/java-arrays-guide[Arrays in Java]


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Fix this access on an array element that may trigger an 'ArrayIndexOutOfBoundException'.

'''

endif::env-github,rspecator-view[]



//=== How does this work?

//=== Pitfalls

//=== Going the extra mile

//=== Articles & blog posts
//=== Conference presentations
//=== Standards
//=== Benchmarks
