Skip to content
Fix Code Error

What could cause java.lang.reflect.InvocationTargetException?

March 13, 2021 by Code Error
Posted By: Anonymous

Well, I’ve tried to understand and read what could cause it but I just can’t get it:

I have this somewhere in my code:

 try{
 ..
 m.invoke(testObject);
 ..
 } catch(AssertionError e){
 ...
 } catch(Exception e){
 ..
 }

Thing is that, when it tries to invoke some method it throws
InvocationTargetException instead of some other expected exception (specifically ArrayIndexOutOfBoundsException).
As I actually know what method is invoked I went straight to this method code and added a try-catch block for the line that suppose to throw ArrayIndexOutOfBoundsException and it really threw ArrayIndexOutOfBoundsException as expected. Yet when going up it
somehow changes to InvocationTargetException and in the code above catch(Exception e)
e is InvocationTargetException and not ArrayIndexOutOfBoundsException
as expected.

What could cause such a behavior or how can I check such a thing?

Solution

You’ve added an extra level of abstraction by calling the method with reflection. The reflection layer wraps any exception in an InvocationTargetException, which lets you tell the difference between an exception actually caused by a failure in the reflection call (maybe your argument list wasn’t valid, for example) and a failure within the method called.

Just unwrap the cause within the InvocationTargetException and you’ll get to the original one.

Answered By: Anonymous

Related Articles

  • Unable to run Robolectric and Espresso with a…
  • Eclipse will not start and I haven't changed anything
  • How can I resolve Web Component Testing error?
  • When I'm testing a web app by JUnit and Mockito I…
  • Can't access Eclipse marketplace
  • ClassNotFoundException:…
  • TLS 1.3 server socket with Java 11 and self-signed…
  • Neither BindingResult nor plain target object for…
  • JPA Hibernate Persistence exception…
  • Examples of GoF Design Patterns in Java's core libraries
  • Using enums in a spring entity
  • java.lang.RuntimeException: Unable to instantiate…
  • SQLException: No suitable Driver Found for…
  • How to parse JSON file with Spring
  • NullpointerException error while working with…
  • Storing Objects in HTML5 localStorage
  • UnsatisfiedDependencyException: Error creating bean…
  • Gradle error: Execution failed for task…
  • JUNIT @ParameterizedTest , Parameter resolution Exception
  • java IO Exception: Stream Closed
  • Configure hibernate to connect to database via JNDI…
  • "Non-resolvable parent POM: Could not transfer…
  • Launching Spring application Address already in use
  • Jetpack Compose and Hilt Conflict
  • What is a NullReferenceException, and how do I fix it?
  • Export more than one variable in ES6?
  • SecurityException: Permission denied (missing…
  • Spring Boot with ElasticSearch in Groovy: WebClient…
  • Requested bean is currently in creation: Is there an…
  • Error inflating class android.support.v7.widget.Toolbar?
  • How can I throw CHECKED exceptions from inside Java…
  • Mock MQRFH2 header in JUnit Testing Error [MQRFH2…
  • Execution failed for task…
  • Why does Spring security throw exception…
  • Eclipse fails to open .vue files in Web Page Editor
  • android studio 0.4.2: Gradle project sync failed error
  • How to add Typescript to a Nativescript-Vue project?
  • org.gradle.api.tasks.TaskExecutionException:…
  • java.lang.ClassNotFoundException: HttpServletRequest
  • TypeScript metadata reflection references other…
  • What is a stack trace, and how can I use it to debug…
  • ClassNotFoundException thrown
  • How do I install Java on Mac OSX allowing version switching?
  • Injection of autowired dependencies failed;
  • Do you (really) write exception safe code?
  • Android- Error:Execution failed for task…
  • Name [jdbc/mydb] is not bound in this Context
  • How to convert java.util.Date to java.sql.Date?
  • Pentaho Data Integration SQL connection
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Spring Security exclude url patterns in security…
  • Vaadin Spring Boot - There was an exception while…
  • Spring Boot - Cannot determine embedded database…
  • Java.lang.NoClassDefFoundError:…
  • Reference - What does this regex mean?
  • Could not install Gradle distribution from…
  • Android Studio 4.2.1 NullPointer Exception on startup
  • What's causing my java.net.SocketException:…
  • Row was updated or deleted by another transaction…
  • com.mysql.jdbc.exceptions.jdbc4.CommunicationsExcept…
  • Plugin…
  • How to filter a RecyclerView with a SearchView
  • What does "Could not find or load main class" mean?
  • How to disable SSL certificate checking with Spring…
  • What are the undocumented features and limitations…
  • Problems using Maven and SSL behind proxy
  • javax.faces.application.ViewExpiredException: View…
  • Javax.net.ssl.SSLHandshakeException:…
  • What's the difference between Instant and LocalDateTime?
  • Failed to execute goal…
  • Java ElasticSearch None of the configured nodes are…
  • READ_EXTERNAL_STORAGE permission for Android
  • Spring schemaLocation fails when there is no…
  • IOException: read failed, socket might closed -…
  • Getting Dropwizard Client And Jersey/HTTP I/O Error…
  • Ukkonen's suffix tree algorithm in plain English
  • How to use Servlets and Ajax?
  • What is the scope of variables in JavaScript?
  • Got exception when running the localhost cluster
  • Fastest way to iterate over all the chars in a String
  • Eclipse - Run in Debug Mode fails
  • What's the difference between eval, exec, and compile?
  • Identifying and solving…
  • Exception : AAPT2 error: check logs for details
  • How to use java.net.URLConnection to fire and handle…
  • Non-resolvable parent POM for Could not find…
  • What is an IndexOutOfRangeException /…
  • Why am I getting a "401 Unauthorized" error in Maven?
  • How to pass type value to a variable with the type…
  • commandButton/commandLink/ajax action/listener…
  • How to test Spring Data repositories?
  • org.springframework.beans.factory.NoSuchBeanDefiniti…
  • java.lang.IllegalStateException: Can not perform…
  • Multipart File Upload Using Spring Rest Template +…
  • Integrate ZXing in Android Studio
  • Android Room - simple select query - Cannot access…
  • Throw HttpResponseException or return…
  • What is your most productive shortcut with Vim?
  • Jackson cannot parse body of a POST request
  • SQL Error: 0, SQLState: 08S01 Communications link failure

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Why do I need to do `–set-upstream` all the time?

Next Post:

How to align checkboxes and their labels consistently cross-browsers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error