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

This article has been dead for over six months. Start a new discussion instead.