944,119 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5840
  • Java RSS
Aug 6th, 2005
0

Invoking

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Rete is offline Offline
23 posts
since Jun 2005
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how to create application
Next Thread in Java Forum Timeline: UnsupportedFlavorException???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC