Dynamic class loading problem ...

Discussion in 'Java' started by biomechanism, May 22, 2004.

  1. biomechanism

    biomechanism Guest

    Hi,

    I've been trying to write some test code for dynamic class loading.
    Basically, I have a JTabbedPane, and I want to dynamically add
    panels (JPanel)to it.

    There is however a problem trying to instantiate the class after it
    has been loaded, I get an exception.

    Actually, this is only true if I try to use a constructor.
    when I leave out the constructor code I can call a method to
    do the initialisation after instantiation, but this isn't
    really what I want.

    Has anyone had this problem, or any suggestions on fixing it?


    Regards,
    Shane.
    --
    [PGP: 035e63e3]
     
    biomechanism, May 22, 2004
    #1
    1. Advertising

  2. biomechanism

    Sudsy Guest

    biomechanism wrote:
    <snip>
    > Actually, this is only true if I try to use a constructor.
    > when I leave out the constructor code I can call a method to
    > do the initialisation after instantiation, but this isn't
    > really what I want.
    >
    > Has anyone had this problem, or any suggestions on fixing it?


    Without seeing the code, it's difficult to address. I use reflection
    quite a bit and haven't encountered any problems. The mechanism can
    be a bit confusing at first but I've never seen a "bug" in the
    implementation.
    Could you post your offending code?
     
    Sudsy, May 22, 2004
    #2
    1. Advertising

  3. biomechanism

    biomechanism Guest

    On Sat, 22 May 2004 15:17:23 -0400, Sudsy wrote:

    [snip]
    > Could you post your offending code?


    Sure:

    [============== Framework class =============]

    package testpak;

    import javax.swing.*;
    import testpak.gui.Test;
    import java.awt.*;

    public class Framework {
    public static void main(String argv[])
    {
    guiwork gw = new guiwork();
    testpak.gui.Test tst;

    try {
    System.out.println("Trying to load Test class ... ");
    Class testclass = Class.forName("testpak.gui.Test");
    System.out.println("Trying to Instantiate Test ... ");
    tst = (testpak.gui.Test) testclass.newInstance();
    gw.addTab(tst, "Mooo");
    }
    catch (Exception e)
    {
    System.out.println("Noooooooo!!");
    }
    gw.show();
    }
    }


    [============== guiwork class =============]

    package testpak;
    import javax.swing.*;

    public class guiwork extends JDialog {
    JTabbedPane tabbase;
    JPanel panel1;
    JPanel panel2;

    guiwork()
    {
    tabbase = new JTabbedPane();
    panel1 = new JPanel();
    panel2 = new JPanel();
    tabbase.add("One", panel1);
    tabbase.add("Two", panel2);
    add(tabbase);
    setSize(640, 480);
    show();
    }

    public void addTab(testpak.gui.Test p, String s)
    {
    tabbase.add(s, p);
    }

    }


    [============== Test class =============]

    package testpak.gui;

    import javax.swing.*;

    public class Test extends JPanel{

    JLabel label = new JLabel("Mooooo");

    // Test()
    // {
    // add(label);
    // }

    public void doSomething()
    {
    add(label);
    }
    }


    This 'Test' class is in a subdirectory called gui.
    If I uncomment the constructor the Test class will
    fail to instantiate after it has been loaded.

    I'm a bit puzzled.


    Regards,
    Shane.
    --
    [PGP: 035e63e3]
     
    biomechanism, May 22, 2004
    #3
  4. biomechanism

    Sudsy Guest

    biomechanism wrote:
    <snip>
    > I'm a bit puzzled.
    >
    >
    > Regards,
    > Shane.


    Oh boy, are you ever going to be red-faced when you learn the
    solution! Your constructor has to be public. In Test, simply
    changing the no-argument constructor to:
    public Test() {
    ...
    solves the perceived problem.
    See? You didn't find a bug after all.
    Best wishes in your learning experience. Java is actually a
    ton of fun, once you get used to it.
     
    Sudsy, May 22, 2004
    #4
  5. biomechanism

    biomechanism Guest

    On Sat, 22 May 2004 16:37:19 -0400, Sudsy wrote:

    > Oh boy, are you ever going to be red-faced when you learn the
    > solution!


    Hehe.
    Arse. :)

    > Your constructor has to be public. In Test, simply
    > changing the no-argument constructor to:
    > public Test() {
    > ...
    > solves the perceived problem.
    > See? You didn't find a bug after all.
    > Best wishes in your learning experience. Java is actually a
    > ton of fun, once you get used to it.


    Cheers, I won't have to smack my head off stuff now. :)


    Regards,
    Shane.
    --
    [PGP: 035e63e3]
     
    biomechanism, May 23, 2004
    #5
  6. biomechanism

    Sudsy Guest

    biomechanism wrote:
    > Hehe.
    > Arse. :)


    > Shane.


    Since you were good enough to post the entire code, I spent
    the time to paste it into my servlet container and test.
    What I meant was no disservice. I love Java and all the things
    it can do for you. It just takes a bit of time to realize all
    the benefits.
    Truth be told, it took me about six months before I was able
    to truly "think" in object-oriented terms.
    I came from a background which included writing VTAM (Assembler
    G and H) code! So it just takes time to wrap the mind around a
    fundamentally different paradigm.
    No need to beat your head against the wall when you have people
    out there willing to provide assistance.
    You made the big jump by providing code. If we have that then
    we can perform the appropriate analysis.
    So you learned, and the greater community can benefit from the
    exchange. Sounds like a winning combination to me...
     
    Sudsy, May 23, 2004
    #6
  7. biomechanism

    biomechanism Guest

    On Sun, 23 May 2004 10:23:18 -0400, Sudsy wrote:

    > Since you were good enough to post the entire code, I spent
    > the time to paste it into my servlet container and test.


    Cheers, your time was/is appreciated.

    > What I meant was no disservice. I love Java and all the things
    > it can do for you. It just takes a bit of time to realize all
    > the benefits.
    > Truth be told, it took me about six months before I was able
    > to truly "think" in object-oriented terms.


    Well, I'm not really new to OO, but I haven't done much OO
    programming, other than previous Uni. stuff. I have a C++ project
    on hold, as I'm doing an MSc project ATM, which I've decided to
    do in Java, as I want to learn the language anyway. There's quite
    a bit I like about it.

    > I came from a background which included writing VTAM (Assembler
    > G and H) code! So it just takes time to wrap the mind around a
    > fundamentally different paradigm.


    I like assembly, not great at it, but it's fun.

    > No need to beat your head against the wall when you have people
    > out there willing to provide assistance.


    I appreciated all people who participate in helping others out,
    I've learned plenty from other posts in various areas, whether
    directly related to me or not.

    > You made the big jump by providing code.


    Not really sure what you mean by that.

    > If we have that then
    > we can perform the appropriate analysis.
    > So you learned, and the greater community can benefit from the
    > exchange. Sounds like a winning combination to me...


    Well, if anyone other than myself benefited from it, then I'd be
    even more happy.


    Regards,
    Shane.
    --
    [PGP: 035e63e3]
     
    biomechanism, May 24, 2004
    #7
  8. On Mon, 24 May 2004 02:47:10 +0100, biomechanism wrote:

    >> You made the big jump by providing code.

    >
    > Not really sure what you mean by that.


    There are, believe it or not, people who
    will argue all ways till Sunday why they
    should not 'have' to provide an example.

    Yes, ..wasting more time arguing why they
    _should not_, rather than spending the time
    necessary to prepare an example.

    Yet they expect a precise, specific answer.
    Go figure. ;-)

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, May 24, 2004
    #8
    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. E11
    Replies:
    1
    Views:
    4,895
    Thomas Weidenfeller
    Oct 12, 2005
  2. Denis Nikiforov

    cocoon + dynamic class loading

    Denis Nikiforov, Mar 18, 2006, in forum: Java
    Replies:
    1
    Views:
    388
    Denis Nikiforov
    Mar 18, 2006
  3. Aguilar, James

    Dynamic Class Loading

    Aguilar, James, Jul 14, 2004, in forum: C++
    Replies:
    17
    Views:
    900
    Phlip
    Jul 16, 2004
  4. Richard Maher
    Replies:
    16
    Views:
    832
    Chris Uppal
    Feb 20, 2007
  5. Goofball
    Replies:
    1
    Views:
    961
    Tom Hawtin
    Jun 11, 2007
Loading...

Share This Page