Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Reflection - Invoking method returns an exception

  1. #1
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default Reflection - Invoking method returns an exception

    I keep getting this error whenever i invoke a method:
    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)
    Here is my code (ignore the "AppletClass part"):
    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);
    Im trying to get the mouseDrag method so i could send events to it.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    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:

    Java Code:
    Object o = Gr.invoke(AppletClass.getComponentAt(1. 1), Event.MOUSE_MOVE, 100, 100);
    and not on a Class object as you did. Also read the API documentation for the Method class; it's all in there ...

    kind regards,

    Jos
    JeffGrigg likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    object is not an instance of declaring class
    It isn't. It's an instance of java.lang.Class.

    db

    edit Grrrr...

  4. #4
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    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!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by crikey View Post
    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!
    Read the API documentation for the MouseEvent class; create one and pass it to your method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Reflection Invoking method of "super" class
    By Shellback3 in forum Advanced Java
    Replies: 2
    Last Post: 01-31-2011, 05:50 PM
  2. Invoking a method
    By Brainz in forum New To Java
    Replies: 6
    Last Post: 12-06-2010, 03:07 PM
  3. Invoking method from String
    By Supamagier in forum Advanced Java
    Replies: 12
    Last Post: 05-29-2009, 10:21 AM
  4. Replies: 0
    Last Post: 04-23-2008, 09:14 PM
  5. Object Reflection: Invoking methods
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •