please, your comments on why this code won't work

Discussion in 'Java' started by learningjava, Dec 11, 2003.

  1. learningjava

    learningjava Guest

    Hello,
    I am trying to use reflection to create a new instance of an inner
    class
    (and hence use it later).
    The code compiles fine, but throws a NoSuchMethodException on runtime.
    Can you please comment on and provide the right approach to
    do this?

    Thanks,

    StrugglingWithReflection

    code:

    ****

    import java.lang.reflect.*;

    public class Reflection {

    private class InnerClass1 {
    //default constructor
    public InnerClass1() { }

    public String description() {
    return "InnerClass 1";
    }
    }

    public static void main(String[] args) throws ClassNotFoundException,
    NoSuchMethodException,
    InstantiationException, IllegalAccessException,
    InvocationTargetException
    {

    //trying to create a new instance of the inner class
    //code compiled with no syntax errors
    Class cls = Class.forName("Reflection$" + "InnerClass1");
    Class[] partypes;
    partypes = new Class[1];
    partypes[0] = Long.TYPE;
    //this throws a NoSuchMethodException (runtime error). why?
    //the exception text also has something like <init>
    //what does it mean?
    Constructor con = cls.getConstructor(partypes);
    Object[] arglist;
    arglist = new Object[1];
    arglist[0] = new Long(500);
    Object methobj = con.newInstance(arglist);
    }
    }


    ****
     
    learningjava, Dec 11, 2003
    #1
    1. Advertising

  2. learningjava wrote:

    > Hello,
    > I am trying to use reflection to create a new instance of an inner
    > class
    > (and hence use it later).
    > The code compiles fine, but throws a NoSuchMethodException on runtime.
    > Can you please comment on and provide the right approach to
    > do this?


    You posted nearly exactly the same question on December 2nd and were given
    two correct answers, which still apply here.
     
    Michael Borgwardt, Dec 11, 2003
    #2
    1. Advertising

  3. learningjava

    Adam Guest

    > public class Reflection {
    >
    > private class InnerClass1 {
    > //default constructor
    > public InnerClass1() { }
    >
    > public String description() {
    > return "InnerClass 1";
    > }

    <cut>
    in main():
    > Class cls = Class.forName("Reflection$" + "InnerClass1");
    > Class[] partypes;
    > partypes = new Class[1];
    > partypes[0] = Long.TYPE;
    > //this throws a NoSuchMethodException (runtime error). why?
    > //the exception text also has something like <init>
    > //what does it mean?
    > Constructor con = cls.getConstructor(partypes);

    I'm not a reflection expert but i guess that this means that you
    want to obtaint a constructor which takes Long as a parameter,
    which you don't have in InnerClass1

    Adam
     
    Adam, Dec 11, 2003
    #3
  4. (learningjava) writes:

    > public InnerClass1() { }


    Do you see a "long xxx" inside those parentheses? Neither do we.

    > Class[] partypes;
    > partypes = new Class[1];
    > partypes[0] = Long.TYPE;
    > //this throws a NoSuchMethodException (runtime error). why?
    > //the exception text also has something like <init>
    > //what does it mean?
    > Constructor con = cls.getConstructor(partypes);


    It means that despite your insistence you haven't written a
    constructor thast takes a long parameter.
     
    Tor Iver Wilhelmsen, Dec 11, 2003
    #4
  5. learningjava

    learningjava Guest

    Tor,

    How do I call a getConstructor on a constructor which has no arguments ?
    And I am getting the same runtime exception even after I add
    the long argumentto the constructor.

    Thanks,

    gk

    > (learningjava) writes:
    >
    > > public InnerClass1() { }

    >
    > Do you see a "long xxx" inside those parentheses? Neither do we.
    >
    > > Class[] partypes;
    > > partypes = new Class[1];
    > > partypes[0] = Long.TYPE;
    > > //this throws a NoSuchMethodException (runtime error). why?
    > > //the exception text also has something like <init>
    > > //what does it mean?
    > > Constructor con = cls.getConstructor(partypes);

    >
    > It means that despite your insistence you haven't written a
    > constructor thast takes a long parameter.
     
    learningjava, Dec 12, 2003
    #5
  6. learningjava <> scribbled the following:
    > Tor,


    > How do I call a getConstructor on a constructor which has no arguments ?
    > And I am getting the same runtime exception even after I add
    > the long argumentto the constructor.


    Have you tried this?

    Constructor con = cls.getConstructor(new Class[0]);

    IOW, just use an empty array of Class references.

    >> (learningjava) writes:
    >>
    >> > public InnerClass1() { }

    >>
    >> Do you see a "long xxx" inside those parentheses? Neither do we.
    >>
    >> > Class[] partypes;
    >> > partypes = new Class[1];
    >> > partypes[0] = Long.TYPE;
    >> > //this throws a NoSuchMethodException (runtime error). why?
    >> > //the exception text also has something like <init>
    >> > //what does it mean?
    >> > Constructor con = cls.getConstructor(partypes);

    >>
    >> It means that despite your insistence you haven't written a
    >> constructor thast takes a long parameter.


    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
    - Kent Brockman
     
    Joona I Palaste, Dec 12, 2003
    #6
  7. learningjava

    learningjava Guest

    > > Hello,
    > > I am trying to use reflection to create a new instance of an inner
    > > class
    > > (and hence use it later).
    > > The code compiles fine, but throws a NoSuchMethodException on runtime.
    > > Can you please comment on and provide the right approach to
    > > do this?

    >
    > You posted nearly exactly the same question on December 2nd and were given
    > two correct answers, which still apply here.


    But I'm still not able to get it working.
    I gave a long argument to my constructor, and tried it.
    Still I get the same exception.
    Can you post the right code?

    Thanks,

    gk
     
    learningjava, Dec 12, 2003
    #7
  8. learningjava

    learningjava Guest

    Ok, I modified my code.
    I was wrong about no providing the proper arguments to the
    constructor.
    That works now, but I am not able to invoke newInstance().
    Here is the modified code :
    ****

    import java.lang.reflect.*;

    public class Reflection {

    private class InnerClass1 {
    //default constructor
    public InnerClass1(int num) {}

    public String description() {
    return "InnerClass 1";
    }
    }

    public static void main(String[] args) throws ClassNotFoundException,
    NoSuchMethodException,
    InstantiationException, IllegalAccessException,
    InvocationTargetException
    {

    //trying to create a new instance of the inner class
    //code compiled with no syntax errors
    Class cls = Class.forName("Reflection$" + "InnerClass1");
    Class[] partypes;
    partypes = new Class[1];
    partypes[0] = Integer.class;
    Object[] arglist;
    arglist = new Object[1];
    arglist[0] = new Integer(500);
    Constructor[] cons = cls.getConstructors();
    System.out.println(cons[0]);
    Object methobj = cons[0].newInstance(arglist);


    }


    }

    ****

    Thanks again for you help,

    gk


    > (learningjava) writes:
    >
    > > public InnerClass1() { }

    >
    > Do you see a "long xxx" inside those parentheses? Neither do we.
    >
    > > Class[] partypes;
    > > partypes = new Class[1];
    > > partypes[0] = Long.TYPE;
    > > //this throws a NoSuchMethodException (runtime error). why?
    > > //the exception text also has something like <init>
    > > //what does it mean?
    > > Constructor con = cls.getConstructor(partypes);

    >
    > It means that despite your insistence you haven't written a
    > constructor thast takes a long parameter.
     
    learningjava, Dec 12, 2003
    #8
  9. learningjava wrote:

    >>>Hello,
    >>>I am trying to use reflection to create a new instance of an inner
    >>>class
    >>>(and hence use it later).
    >>>The code compiles fine, but throws a NoSuchMethodException on runtime.
    >>>Can you please comment on and provide the right approach to
    >>>do this?

    >>
    >>You posted nearly exactly the same question on December 2nd and were given
    >>two correct answers, which still apply here.

    >
    >
    > But I'm still not able to get it working.
    > I gave a long argument to my constructor, and tried it.
    > Still I get the same exception.
    > Can you post the right code?


    The constructors of an inner class each have a hidden argument through
    which a reference to the containing class instance is passed. You need
    to take that into account, but have not done so. To figure out what is
    needed, why don't you use Class.getConstructors() on the inner class,
    and Constructor.getParameterTypes() on each constructor? That way you
    can figure out exactly what is required.

    For example:

    [...]

    Class myClass = Class.forName("mypackage.Foo$InnerFoo");
    Constructor[] constructors = myClass.getConstructors();

    for (int i = 0; i < constructors.length; i++) {
    Class[] types = constructors.getParameterTypes();
    for (int j = 0; j < types.length; j++) {
    System.out.print(types[j]);
    System.out.print(' ');
    }
    System.out.println();
    System.out.println();
    }

    [...]


    John Bollinger
     
    John C. Bollinger, Dec 12, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marina

    Re: WHY, WHY WON'T IT WORK???

    Marina, Jun 29, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    352
    Marina
    Jun 29, 2004
  2. Chad
    Replies:
    4
    Views:
    8,339
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,958
    Smokey Grindel
    Dec 2, 2006
  4. www.jrdesignstudio.com
    Replies:
    7
    Views:
    1,325
    philiptdotcom
    Oct 18, 2007
  5. Mclaren Fan
    Replies:
    2
    Views:
    651
    Richard Cornford
    Nov 8, 2011
Loading...

Share This Page