954,176 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Invoking

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"
	<strong>Object instance = getclass.newInstance();</strong>
	
	//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.
	<strong>Method run = getclass.getMethod(methodname, parametertypes);</strong>
	
	
	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?

Rete
Newbie Poster
23 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 
Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You