• Post Reply Bookmark Topic Watch Topic
  • New Topic

IllegalArgumentException from method.invoke  RSS feed

 
K Dombroski
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to use reflection to call various methods based on values in a properties files.
Here is what I'm trying to accomplish:

Here is my code:

I keep getting an IllegalArgumentException from my method.invoke call. I have put in output statements, and the methods being called look right. I have tried using to pass the parameter types in the getMethod call and then using an Object[] as the 2nd param in the method2.invoke, but that does not work either. I can't figure out what is wrong. The parameter types match what I'm declaring. What could be causing this exception?

Thanks for any help you can provide.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the javadoc
IllegalArgumentException - if the method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or implementor thereof); if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion.
So i guess that the c2 parameter is not the correct object type. A stacktrace would be useful.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
method.invoke(null)

What is this for ?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Invoking the static createXXX method; the DaoFactory.create<DaoCass>() part.

The problem is in the invoking of the second method. The first parameter is a Class object, but it should be the instance. Try to change lines 7-9 to this:
 
K Dombroski
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Rob! That worked!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!