Results 1 to 5 of 5
- 08-02-2011, 06:02 PM #1
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Reflection - Invoking method returns an exception
I keep getting this error whenever i invoke a method:
Here is my code (ignore the "AppletClass part"):Java Code:Exception in thread "main" java.lang.IllegalArgumentException: object is not an instance of declaring class at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.zbot.boot.ZBot.main(ZBot.java:56)
Im trying to get the mouseDrag method so i could send events to it.Java Code:Class clazz = AppletClass.getComponentAt(1, 1).getClass(); Method[] m = clazz.getMethods(); Method Gr = null; for (Method t : m) { if (t.getName().contains("mouseDrag")){ System.out.println(t.toGenericString()); Gr = t; } } Object o = Gr.invoke(clazz, Event.MOUSE_MOVE, 100, 100);
- 08-02-2011, 06:18 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
You should invoke a method on a specific object; you got that Method from the object AppletClass.getComponentAt(1, 1) so that's where you should invoke the method on:
and not on a Class object as you did. Also read the API documentation for the Method class; it's all in there ...Java Code:Object o = Gr.invoke(AppletClass.getComponentAt(1. 1), Event.MOUSE_MOVE, 100, 100);
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-02-2011, 06:22 PM #3
It isn't. It's an instance of java.lang.Class.object is not an instance of declaring class
db
edit Grrrr...
- 08-02-2011, 06:45 PM #4
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Sweet it works :)
Except now, Im getting a IllegalArgumentException, and i know why. That method mouseDrag requires for its parameters a:
Event event
int x
int y
But what am I suppose to put for Event? I tried Event.MOUSE_MOVE but that's an integer. Any thoughts? Thanks!
- 08-02-2011, 08:17 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Reflection Invoking method of "super" class
By Shellback3 in forum Advanced JavaReplies: 2Last Post: 01-31-2011, 04:50 PM -
Invoking a method
By Brainz in forum New To JavaReplies: 6Last Post: 12-06-2010, 02:07 PM -
Invoking method from String
By Supamagier in forum Advanced JavaReplies: 12Last Post: 05-29-2009, 09:21 AM -
Object Reflection: Invoking constructor with parameters
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:14 PM -
Object Reflection: Invoking methods
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:14 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks