Dynamic class loading problem ...

B

biomechanism

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.
 
S

Sudsy

biomechanism wrote:
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?
 
B

biomechanism

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.
 
S

Sudsy

biomechanism wrote:
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.
 
B

biomechanism

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.
 
S

Sudsy

biomechanism said:
Hehe.
Arse. :)

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...
 
B

biomechanism

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.
 
A

Andrew Thompson

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. ;-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top