Exception in thread main java lang indexoutofboundsexception



Array Index Out Of Bounds Exception in Java

Java supports the creation and manipulation of arrays as a data structure. The index of an array is an integer value that has value in the interval [0, n-1], where n is the size of the array. If a request for a negative or an index greater than or equal to the size of the array is made, then the JAVA throws an ArrayIndexOutOfBounds Exception. This is unlike C/C++, where no index of the bound check is done.

The ArrayIndexOutOfBoundsException is a Runtime Exception thrown only at runtime. The Java Compiler does not check for this error during the compilation of a program.

Expected Output:

Original Output:

Runtime error throws an Exception:

Here if you carefully see, the array is of size 5. Therefore while accessing its element using for loop, the maximum index value can be 4, but in our program, it is going till 5 and thus the exception.

Let’s see another example using ArrayList:

Runtime error here is a bit more informative than the previous time-

Let us understand it in a bit of detail:

  • Index here defines the index we are trying to access.
  • The size gives us information on the size of the list.
  • Since the size is 2, the last index we can access is (2-1)=1, and thus the exception.

The correct way to access the array is :

Correct Code –

Handling the Exception:

1. Using for-each loop:

This automatically handles indices while accessing the elements of an array.

Syntax:

Example:

2. Using Try-Catch:

Consider enclosing your code inside a try-catch statement and manipulate the exception accordingly. As mentioned, Java won’t let you access an invalid index and will definitely throw an ArrayIndexOutOfBoundsException. However, we should be careful inside the block of the catch statement because if we don’t handle the exception appropriately, we may conceal it and thus, create a bug in your application.

Here in the above example, you can see that till index 4 (value 5), the loop printed all the values, but as soon as we tried to access the arr[5], the program threw an exception which is caught by the catch block, and it printed the “Exception caught” statement.

Explore the Quiz Question.

Источник

Understanding IndexOutOfBoundsException in Java

In this article, we will learn about IndexOutOfBoundsException in Java.

The IndexOutOfBoundsException in Java is thrown when we try to access an invalid index (beyond its range) in a collection like an array, string, etc.

Now there are two kinds of IndexOutOfBoundsException .

  1. ArrayIndexOutOfBoundsException
  2. StringIndexOutOfBoundsException

IndexOutOfBoundsException is a superclass of both of them. Below is the hierarchy.

Now let’s look at both these exceptions one by one.

the ArrayIndexOutOfBoundsException in Java

We know that array is a data structure that stores similar values, and we use their index locations to access the elements. The intervals are in the range of [0 .. n-1] , where n represents the size of the array.

Suppose you try to access an index location that is either greater than the size of the array or less than zero, i.e., negative index value. In that case, we get the ArrayIndexOutOfBoundsException .

Читайте также:  Unexpected error occurred invoking async method

Note that unlike other languages like C/C++, Java doesn’t perform bound checking of the index.

The ArrayIndexOutOfBoundsException is a runtime exception, meaning the Java compiler throws the exception during the program’s execution, not at compile time. And since it is an unchecked exception, we don’t have to declare it in a method or constructor’s throws clause.

In the above code, the array we created is of size 4, meaning the index will range from 0 to 3 but in the loop, we are trying to access the value present at index 4 and hence the exception.

We can fix this by modifying the for loop to for (int i = 0; i .

Example two: Using the ArrayList collection.

the StringIndexOutOfBoundsException in Java

In Java, strings are just a sequence of characters and are treated as objects of the String class of the java.lang package. We can declare strings in two ways.

Declaring using the new keyword similar to declaring an object.

Declaring using string literals .

Like arrays, the string is also a sequence/array of characters where each character is represented by an index starting from zero.

And as we use to access the elements of an array using its index, in the same we can use the charAt(index) method of the String class to access the character present at that index; here as well, the index should be from 0 to string_size — 1 .

So here, when we try to access a character by specifying an index greater than the string, we get the StringIndexOutOfBoundsException .

In the above code, the string we created is of size 5, meaning the index will range from 0 to 4 but in the loop, we are trying to access the value present at index 5 and hence the exception.

Handling Exceptions in Java

You can use a try-catch block in handling exceptions.

Subclasses of the IndexOutOfBoundsException in Java

We have two subclasses under IndexOutOfBoundsException :

  1. StringIndexOutOfBoundsException class
  2. ArrayIndexOutOfBoundsException class

the StringIndexOutOfBoundsException Class

StringIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException , which is thrown by String methods to indicate that the index is negative or greater than the size of the actual string. In some special cases, like in the case of the charAt() method, this exception is thrown even when the index is equal to the string’s size.

For example, when the index is negative inside the method:

In the above code, we are trying to fetch the character at the -1 index from the string.

We can handle this exception by simply checking the index range before passing it inside the string function. If it is out of range, we throw the StringIndexOutOfBoundsException with a custom message.

the ArrayIndexOutOfBoundsException Class

ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException , which is thrown when the array is accessed with an illegal index whose index is greater or equal to the size of the declared array or the index is negative.

For example, when the index is greater than the size of the array:

In the above code, we are trying to access the array value present at index 10.

We can handle this exception by checking the index range before accessing the array element. If it is out of range, we can throw the ArrayIndexOutOfBoundsException with a custom message.

Читайте также:  Java has protected access error

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

Источник

Java Hungry

Java developers tutorials and coding.

[Solved] java.lang.IndexOutOfBoundsException

In this post, I will be sharing how to fix java.lang.IndexOutOfBoundsException in Java. According to Oracle docs, IndexOutOfBoundsException is a subclass of RuntimeException. It is an unchecked exception that is thrown to indicate that an index of some sort (such as to a string, to a vector, or to an array) is out of range.

What is Index?

[Fixed] java.lang.IndexOutOfBoundsException

First, we will produce the java.lang.IndexOutOfBoundsException before moving on to the solution.

Output:
Exception in thread «main» java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 3

Explanation

We have added three elements to the list. «Alive» is the 1st element at index 0. «is» is the second element at index 1. «Awesome» is the third element at index 2.
When we are executing

We are trying to fetch the element at index 5. But we have elements stored till index 2. Hence, we get the IndexOutOfBoundsException.

Solution

We can easily solve this exception by adding an if block condition that the index should always be smaller than the list size as shown below in the example:

Subclasses of IndexOutOfBoundsException

There are two subclasses of IndexOutOfBoundsException:

1. ArrayIndexOutOfBoundsException

According to Oracle docs, ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException. This exception is thrown to indicate that an array has been accessed with an illegal index. The illegal index means
a. If the index is negative or
b. If the index is greater than or equal to the size of the array

Producing the ArrayIndexOutOfBoundsException

Example1: When the index is negative

Output:
Exception in thread «main» java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 3

Example2: When the index is greater than or equal to the size of the array

Output:
Exception in thread «main» java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 3

Solution

We can easily solve the ArrayIndexOutOfBoundsException by adding an if block condition that the index should always be smaller than the array size as shown below in the example:

Output:
Exception in thread «main» java.lang.ArrayIndexOutOfBoundsException: Invalid Index

2. StringIndexOutOfBoundsException

According to Oracle docs, StringIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException. This exception is thrown by String methods to indicate that an index is either negative or greater than the size of the String. For some methods like the charAt() method, this exception is also thrown when the index is equal to the size of the string.

Producing the StringIndexOutOfBoundsException

Example1: When the index is negative

Output:
Exception in thread «main» java.lang.StringIndexOutOfBoundsException: String index out of range: -4

Example2: When the index is greater than or equal to the size of the String

Output:
Exception in thread «main» java.lang.StringIndexOutOfBoundsException: String index out of range: 23

Читайте также:  Line 24987 error variable used without being declared

Solution

We can easily solve the StringIndexOutOfBoundsException by adding an if block condition that the index should always be smaller than the String size as shown below in the example:

Источник

Java Hungry

Java developers tutorials and coding.

[Solved] java.lang.IndexOutOfBoundsException

In this post, I will be sharing how to fix java.lang.IndexOutOfBoundsException in Java. According to Oracle docs, IndexOutOfBoundsException is a subclass of RuntimeException. It is an unchecked exception that is thrown to indicate that an index of some sort (such as to a string, to a vector, or to an array) is out of range.

What is Index?

[Fixed] java.lang.IndexOutOfBoundsException

First, we will produce the java.lang.IndexOutOfBoundsException before moving on to the solution.

Output:
Exception in thread «main» java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 3

Explanation

We have added three elements to the list. «Alive» is the 1st element at index 0. «is» is the second element at index 1. «Awesome» is the third element at index 2.
When we are executing

We are trying to fetch the element at index 5. But we have elements stored till index 2. Hence, we get the IndexOutOfBoundsException.

Solution

We can easily solve this exception by adding an if block condition that the index should always be smaller than the list size as shown below in the example:

Subclasses of IndexOutOfBoundsException

There are two subclasses of IndexOutOfBoundsException:

1. ArrayIndexOutOfBoundsException

According to Oracle docs, ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException. This exception is thrown to indicate that an array has been accessed with an illegal index. The illegal index means
a. If the index is negative or
b. If the index is greater than or equal to the size of the array

Producing the ArrayIndexOutOfBoundsException

Example1: When the index is negative

Output:
Exception in thread «main» java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 3

Example2: When the index is greater than or equal to the size of the array

Output:
Exception in thread «main» java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 3

Solution

We can easily solve the ArrayIndexOutOfBoundsException by adding an if block condition that the index should always be smaller than the array size as shown below in the example:

Output:
Exception in thread «main» java.lang.ArrayIndexOutOfBoundsException: Invalid Index

2. StringIndexOutOfBoundsException

According to Oracle docs, StringIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException. This exception is thrown by String methods to indicate that an index is either negative or greater than the size of the String. For some methods like the charAt() method, this exception is also thrown when the index is equal to the size of the string.

Producing the StringIndexOutOfBoundsException

Example1: When the index is negative

Output:
Exception in thread «main» java.lang.StringIndexOutOfBoundsException: String index out of range: -4

Example2: When the index is greater than or equal to the size of the String

Output:
Exception in thread «main» java.lang.StringIndexOutOfBoundsException: String index out of range: 23

Solution

We can easily solve the StringIndexOutOfBoundsException by adding an if block condition that the index should always be smaller than the String size as shown below in the example:

Источник

Оцените статью
toolgir.ru
Adblock
detector