Invoking

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Solved Threads: 1
Rete Rete is offline Offline
Newbie Poster

Invoking

 
0
  #1
Aug 6th, 2005
Sorry for troubling everyone with my questions these past few days, but I keep bumping into all these weird little problems.
The code below, is a sort of minor reflective program that runs methods after reading classes.
try {

	//getclass is a class named "Example"
	Object instance = getclass.newInstance();
	
	//Is the name of a method inside "Example"
	String methodname = "returnconcat";
	
	//returnconcat needs 2 strings.
	Class[] parametertypes = new Class[] {String.class, String.class};
	
	//Problem - Says its not an instance, so I have to make an
	//instance
	//Thats what object instance is.  But I cant turn the object
	//into a method.
	Method run = getclass.getMethod(methodname, parametertypes);
	
	
	String result;
         Object[] params = {"hello", "world"};
	result = (String) run.invoke(run, params);
} catch (Exception e) {
	
	e.printStackTrace();
}

The problem I'm getting is that when I run this line of code:
Method run = getclass.getMethod(methodname, parametertypes);
I get a:
java.lang.IllegalArgumentException object is not an instance of declaring class.

So when I make an instance of that with Object instance = getclass.newInstance(); I have no clue what to do with it because it's an object and if I try to cast it as a method, I get a Casting error. Can anybody help me get this to work?
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 765
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: Invoking

 
0
  #2
Aug 7th, 2005
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC