Results 1 to 13 of 13
  1. #1
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default Invoking method from String

    Hello,

    I have this code (it does compile)
    Java Code:
    import java.lang.reflect.*;
    
    public class Example
    {
    
        private final Object waitObj = new Object();
    
        public static void main(String[] args) {
            new Example();
        }
        
        private Example() {
            waitForState(waitObj,getMethod(this,"initialized"));
            System.out.println("Done");
        }
    
        private void waitForState(final Object waitObj, final Method method) {
            try {
                synchronized (waitObj) {
                    while ((new Boolean(true)).equals(method.invoke(method.getDeclaringClass(),new Object[] {})))
                        waitObj.wait();
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private Method getMethod(final Object obj, final String method) {
            try {
                return obj.getClass().getMethod(method,new Class[]{});
            }
            catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        
        public boolean initialized() {
            return true;
        }
    
    }
    I want it to wait until the method initialized() returns true.

    This throws the following error:
    java.lang.IllegalArgumentException: object is not an instance of declaring class
    How to fix this? If possible
    Last edited by Supamagier; 05-28-2009 at 05:46 PM.
    I die a little on the inside...
    Every time I get shot.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    What is Misc?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Java Code:
    public class Example
    {    
        public static void main(String[] args) {
            frame = new MyFrame(this);
    What's this, then?

    Post a SSCCE - something brief that actually yields the compiler message you mention when anyone tries to compile it.
    Last edited by pbrockway2; 05-26-2009 at 12:39 AM.

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    MyFrame is a frame that extends JFrame - but that's not really important. Misc is the class where the getMethod method is in -not really important either, i guess.

    This is the waitforstate method (in misc)
    Java Code:
    public static void waitForState(final Object waitObj, final Method method) {
            try {
                synchronized (waitObj) {
                    while ((new Boolean(true)).equals(method.invoke(method.getDeclaringClass(),new Object[] {})))
                        waitObj.wait();
                }
            }
            catch (Exception e) {
                error(e);
            }
        }
    I die a little on the inside...
    Every time I get shot.

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    where does your illegal argument exception get thrown? Is it inside waitForState or inside getMethod?
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Which method is throwing the exception: look at the stack trace.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    My trouble is in the method 'Misc.getMethod(Object,String)'
    That method ^
    I die a little on the inside...
    Every time I get shot.

  8. #8
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Well nothing in that method declares itself as throwing that exception, and no-one can test it because the code you've provided won't compile.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  9. #9
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    I suppose you've made a point there.

    Main post edited, compilable code.
    I die a little on the inside...
    Every time I get shot.

  10. #10
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    As the stack trace tells you, the exception is actually thrown by Method.invoke() in Example.waitForState()

    You have to call a method on the actual object, not an instance of Class.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  11. #11
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    I'm afraid you lost me there - what should I change?
    has it something to do with this in the waitforstate method?
    Java Code:
    method.getDeclaringClass()
    I die a little on the inside...
    Every time I get shot.

  12. #12
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Yes, read the API docs for Method.invoke().

    The real question is why are you using reflection to do all this, instead of just calling initialised() directly?
    Java Code:
    while (!obj.initialised()) {
        obj.wait();
    }
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  13. #13
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Because I want it to work for any boolean method. ;)
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. Invoking a superclass version of a overridden method
    By CyberFrog in forum New To Java
    Replies: 3
    Last Post: 05-25-2009, 02:33 PM
  2. Entering string in a loop with method
    By Konex in forum New To Java
    Replies: 3
    Last Post: 03-01-2009, 08:33 AM
  3. Method for String to int conversion
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 07:35 PM
  4. invoking method and output questions
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:36 AM
  5. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 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
  •