| | |
Invoking
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2005
Posts: 23
Reputation:
Solved Threads: 1
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.
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?
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
- Perl MIME handler -> how to get the invoking filehandle? (Perl)
- Floating point numbers (C++)
- GUI Window - Exit Button? (Java)
- Question about synchronized and wait (Java)
- TextBox - how to determine focus (ASP.NET)
- Question about read method of InputStream (Java)
- limitations of 10 employees per page (C)
- Error message when I run my program in C++ (C++)
- CPU Usage (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: how to create application
- Next Thread: UnsupportedFlavorException???
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows





