Java reflection multiple parameters -
i trying use java reflection , have 2 different methods call. 1 of them has single string parameter , second 1 has 2 string parameters. i've managed first 1 working, still struggling second one. i've checked references 2 other questions (java reflection: getmethod(string method, object[].class) not working , how invoke method variable arguments in java using reflection?), unfortunately had no luck them. keep getting following exception:
java.lang.nosuchmethodexception: controllers.inventorycontroller.combineitems([ljava.lang.string;) @ java.lang.class.getmethod(unknown source)
here working part of code:
class[] paramstring = new class[1]; paramstring[0] = string.class; try { class cls = this.getclass(); method method = cls.getdeclaredmethod(commandparts[0], paramstring); method.invoke(this, new string(commandparts[1])); } catch (exception ex) { system.out.println("doesn't work"); ex.printstacktrace(); }
now here part can't work:
class[] paramstring = new class[2]; paramstring[0] = string[].class; try { class cls = this.getclass(); method method = cls.getmethod(commandparts[0], paramstring[0]); method.invoke(this, new string[]{commandparts[1], commandparts[2]}); } catch (exception ex) { system.out.println("doesn't work"); ex.printstacktrace(); }
what correct way of passing multiple parameters?
error because of
method method = cls.getmethod(commandparts[0], paramstring[0]);
this says return method name 'commandparts[0]' has 1 parameter of type 'paramstring[0]' change with
method method = cls.getmethod(commandparts[0], string.class, string.class);
Comments
Post a Comment