[Solved] java.lang.reflect.InvocationTargetException in Java with examples

In this post, I will be sharing how to fix java.lang.reflect.InvocationTargetException in Java with examples. According to Oracle docs, java.lang.reflect.InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor. This exception is quite common when using the Java Reflection API. In simple words, the Java reflection layer wraps any exception as an InvocationTargetException.

Read Also: How to fix java.io.NotSerializableException in Java


Let's dive deep into the topic:

[Fixed] java.lang.reflect.InvocationTargetException in Java

As always, first, we will produce the java.lang.reflect.InvocationTargetException before moving on to the solution.

1. Producing the exception


We can easily produce the java.lang.reflect.InvocationTargetException by calling the main() method that in turn invokes throwNPE() method using Method.invoke() as shown below in the example. Since throwNPE() throws a NullPointerException, it is wrapped within an InvocationTargetException thrown in the main() method:

 import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

public class InvocationTargetExceptionExample 
{
    public void throwNPE()
    {
        String str = null;
        str = str.toLowerCase();
    }
    public static void main(String args[])  throws NoSuchMethodException, IllegalAccessException 
    {
        InvocationTargetExceptionExample ex = new InvocationTargetExceptionExample();  
        Method method = InvocationTargetExceptionExample.class.getMethod("throwNPE");  
        try 
        {
            method.invoke(ex);
        }
        catch (InvocationTargetException e) 
        {
            e.printStackTrace();
        }  
    }
}


Output:
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at InvocationTargetExceptionExample.main(InvocationTargetExceptionExample.java:17)
Caused by: java.lang.NullPointerException: Cannot invoke "String.toLowerCase()" because "<local1>" is null
    at InvocationTargetExceptionExample.throwNPE(InvocationTargetExceptionExample.java:9)
    ... 5 more


Solution


The actual cause of the java.lang.reflect.InvocationTargetException is the underlying exception, so, finding and resolving the underlying exception resolves the java.lang.reflect.InvocationTargetException. The Throwable class's getCause() method can be used to obtain the underlying exception as shown below in the example:

java.lang.reflect.InvocationTargetException


 import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

public class InvocationTargetExceptionExample 
{
    public void throwNPE()
    {
        String str = null;
        str = str.toLowerCase();
    }
    public static void main(String args[])  throws NoSuchMethodException, IllegalAccessException 
    {
        InvocationTargetExceptionExample ex = new InvocationTargetExceptionExample();  
        Method method = InvocationTargetExceptionExample.class.getMethod("throwNPE");  
        try 
        {
            method.invoke(ex);
        }
        catch (InvocationTargetException e) 
        {
            Throwable underlyingException = e.getCause();
            underlyingException.printStackTrace();
        }  
    }
}


Output:
java.lang.NullPointerException: Cannot invoke "String.toLowerCase()" because "<local1>" is null
    at InvocationTargetExceptionExample.throwNPE(InvocationTargetExceptionExample.java:9)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at InvocationTargetExceptionExample.main(InvocationTargetExceptionExample.java:17)


That's all for today, please mention in the comments if you have any questions related to how to fix java.lang.reflect.InvocationTargetException in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry