Blog |

How to Fix ArrayStoreException in Java

How to Fix ArrayStoreException in Java
Table of Contents

An ArrayStoreException is a runtime exception in Java that occurs when an attempt is made to store the incorrect type of object into an array of objects. For example, if an Integer object is attempted to be stored in an String array, a “java.lang.ArrayStoreException: java.lang.Integer” is thrown.

 

What Causes ArrayStoreException in Java

The ArrayStoreException occurs when an attempt is made to store the wrong type of object into an array of objects. Here's an example of an ArrayStoreException thrown when an Integer is attempted to be stored in an array of type String:

public class ArrayStoreExceptionExample {
    public static void main(String[] args) {
        Object[] array = new String[2];
        array[0] = 5;
    }
}

Running the above code produces the following output:

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
    at ArrayStoreExceptionExample.main(ArrayStoreExceptionExample.java:4)

 

How to Handle ArrayStoreException in Java

The ArrayStoreException can be handled in code using the following steps:

  • Surround the piece of code that can throw an ArrayStoreException in a try-catch block.
  • Catch the ArrayStoreException in the catch clause.
  • Take further action as necessary for handling the exception and making sure the program execution does not stop.

Here's an example of how to handle it in code:

public class ArrayStoreExceptionExample {
    public static void main(String[] args) {
        try {
            Object[] array = new String[2];
            array[0] = 5;
        } catch (ArrayStoreException ase) {
            ase.printStackTrace();
            //handle the exception
        }
        System.out.println("Continuing execution...");
    }
}

In the above example, the lines that throw the ArrayStoreException are placed within a try-catch block. The ArrayStoreException is caught in the catch clause and its stack trace is printed to the console. Any code that comes after the try-catch block continues its execution normally.

Running the above code produces the following output:

java.lang.ArrayStoreException: java.lang.Integer
    at ArrayStoreExceptionExample.main(ArrayStoreExceptionExample.java:5)
Continuing execution...

 

How to Avoid ArrayStoreException in Java

Since the ArrayStoreException occurs when an object of the wrong data type is added to an array, using the proper data type or casting the object to the correct type can help avoid the exception.

Also, if an array is declared as a specific type, for example String or Integer, instead of a generic type like Object, the compiler will ensure that the correct type is added to the array in code. This can be useful to avoid the ArrayStoreException during runtime.

 

Track, Analyze and Manage Errors With Rollbar

Rollbar in action

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape