Blog |

How to Resolve the Instantiation Exception in Java

How to Resolve the Instantiation Exception in Java
Table of Contents
Try Rollbar Today

Proactively discover and resolve errors

The InstantiationException is a runtime exception in Java that occurs when an application attempts to create an instance of a class using the Class.newInstance() method, but the specified class object cannot be instantiated.

Since the InstantiationException is an unchecked exception, it does not need to be declared in the throws clause of a method or constructor.

 

What Causes InstantiationException

The InstantiationException is thrown when the JVM cannot instantiate a type at runtime. This can happen for a variety of reasons, including the following:

  • The class object represents an abstract class, interface, array class, primitive or void.
  • The class has no nullary constructor. Such a constructor is required when a parameterized constructor is defined for the class.

 

InstantiationException Example

Here is an example of an InstantiationException thrown when the Class.newInstance() method is used to create an instance of a boolean:

public class InstantiationExceptionExample {
    public static void main(String[] args) {
        try {
            Class<Boolean> clazz = boolean.class; 
            clazz.newInstance();
        } catch (InstantiationException ie) {
            ie.printStackTrace();
        } catch (IllegalAccessException iae) {
            iae.printStackTrace();
        }
    }
}

Since boolean is a primitive data type, a new instance of it cannot be created using the Class.newInstance() method, which can only construct objects for concrete classes. Running the above code throws the following exception:

java.lang.InstantiationException: boolean
    at java.base/java.lang.Class.newInstance(Class.java:598)
    at InstantiationExceptionExample.main(InstantiationExceptionExample.java:5)
Caused by: java.lang.NoSuchMethodException: boolean.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3427)
    at java.base/java.lang.Class.newInstance(Class.java:585)
    ... 1 more

 

How to Resolve InstantiationException

To avoid the InstantiationException, it should be ensured that the instance of the class that is attempted to be created at runtime using Class.newInstance() is a concrete class and not an abstract class, interface, array class, primitive or void.

If it is a concrete class, it should be ensured that the class has a nullary constructor (in case it contains a parameterized constructor). If this is not possible, the Constructor objects can be reflectively looked up and used to construct a new instance of the class using Constructor.newInstance(args) with arguments that pass the actual constructor argument values.

 

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 Posts

See all posts

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

Start continuously improving your code today.

Get Started Shape