Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem instantiating an overloaded constructor using Reflections ...

 
Aejaz Sheriff
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem instantiating an overloaded constructor using Reflections ...
I am trying to access a class by passing its name as string using reflections. There are two class below.The problem is that I am getting a java.lang.IllegalArgumentExceptionwhen I try to call the contructor with arguments.
Any help on this would be appreciated.
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getParameterTypes() returns you an array of classes (Class []). In your case, this array will have one element containing the String class. The newInstance() method expects an array of objects (Object []). What you need to do is create an Object array of size one, and put an actual String (not the String class), or null in there. Then use that array as the parameter to newInstance().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic