This discussion is archived
4 Replies Latest reply: Mar 9, 2010 11:28 AM by 843798 RSS

Trying to learn howTo use this stuff but..

843798 Newbie
Currently Being Moderated
My eyes are dim - I cannot see ...
Whatever is wrong with this code snippet:
 
       Person p = (Person) displayList.getSelectedValue();
        Object [] parms = {p};
        System.out.println(p.getClass().getName());
        Class[] parmTs = new Class[1];
        parmTs[0] = p.getClass();
        try {
            Method m = callingClass.getMethod("selected", parmTs);
            System.out.println (m.toGenericString());
*           m.invoke(callingClass, parms);  // callingClass.selected(p);
        } catch (Exception ex) {
            Logger.getLogger(SearchListHandler.class.getName()).log(Level.SEVERE, null, ex);
            return;
        } 
these lines from the System.outs
orchard.Person
public void orchard.TeamWorkSection.selected(orchard.Person)
-- so I have the right object of the correct class and a proper parameter
java.lang.IllegalArgumentException: object is not an instance of declaring class - from line (*)
-- is not what I had in mind!
Help me please!

Thank you for your time.

dkr
  • 1. Re: Trying to learn howTo use this stuff but..
    843798 Newbie
    Currently Being Moderated
    Doug_R wrote:
    java.lang.IllegalArgumentException: object is not an instance of declaring class - from line (*)
    The first argument of invoke() is the object you want to call the method on. You pass in a Class object. Since the method m describes is not a method of Class, it will break.

    You need to pass in an instance of the class represented by callingClass (unless "selected" is a static method, in which case you may pass in null).
  • 2. Re: Trying to learn howTo use this stuff but..
    843798 Newbie
    Currently Being Moderated
    Thank you for the quick reply!

    I am sorry but I still do not understand! In my example,
    pulled from a operating program, callingClass is defined like this:

    private Class callingClass = null;

    but the object referenced by callingClass 'is a' "orchard.TeamWorkSection".

    Are you telling me that:

    Method m = callingClass.getMethod("selected", parmTs);

    can tell that callingClass refers to an orchard.TeamWorkSection but

    m.invoke (callingClass, parms)

    cannot?

    Thanks again;

    Doug R
  • 3. Re: Trying to learn howTo use this stuff but..
    843798 Newbie
    Currently Being Moderated
    Doug_R wrote:
    pulled from a operating program, callingClass is defined like this:

    private Class callingClass = null;
    Yes, I assumed as much. "callingClass" is of type "Class". At some point it must have been assigned a value other than null, or your code would produce a NullPointerException, however.
    but the object referenced by callingClass 'is a' "orchard.TeamWorkSection".
    So callingClass references a Class object representing orchard.TeamWorkSection or one of its super classes.
    Are you telling me that:

    Method m = callingClass.getMethod("selected", parmTs);

    can tell that callingClass refers to an orchard.TeamWorkSection but

    m.invoke (callingClass, parms)

    cannot?
    No. Both know which type of object you want to operate on (the first one, because you explicitly mention it by using callingClass and the second one because the Method object knows on which class the method is declared).

    What the second line does not know is what object you want to call that method on.

    Or put differently: if you call a (non-static) method you provide two things: the object reference and the method name (let's skip the parameters for now).

    .invoke() also tries to call a method. You provide two pieces of information: the method you want to call and the class you want to call it on. But it doesn't need the class (it already knows that), but needs the object instead!

    On which object do you want to call the select() method?
  • 4. Re: Trying to learn howTo use this stuff but..
    843798 Newbie
    Currently Being Moderated
    YES! Thanks

    It was a not so little problem with initialization. I forgot
    what I had to do to get rid of a compile time error.
    It was wrong, wrong, wrong!
    This lead me astray! Old age is setting in rapidly.

    This will work fine. Thanks again for your time!
    dkr