IllegalArgumentException when calling invoke method using Java Reflections -
IllegalArgumentException when calling invoke method using Java Reflections -
i have class has method follows :-
public void setcurrencycode(list<string> newcurrencycode){ this.currencycode = newcurrencycode; } i using java relections invoke method follows :-
try { list<string> value = new arraylist<string>(); value.add("gb"); class<?> clazz = class.forname( "com.xxx.currency" ); object obj = clazz.newinstance(); class param[] = { list.class }; method method = obj.getclass().getdeclaredmethod( "setcurrencycode", param ); method.invoke( value ); } catch(exception e) { system.out.println( "exception : " + e.getmessage() ); } however, exception raised on "invoke" phone call :- java.lang.illegalargumentexception: object not instance of declaring class
any ideas?
thanks
sarah
this means value object pass invoke not instance of class on method defined. because first argument of invoke object on make call, , subsequent arguments parameters invoked method. (in case looks value needs instance of com.xxx.currency - of course of study isn't, because it's list.)
since you're calling non-static method (and going to problem of creating new instance), reflective equivalent of obj.setcurrencycode(value), @ end of seek block you'd need call
method.invoke(obj, value) instead of current single one-arg call.
java reflection invoke
Comments
Post a Comment