Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Dynamic class loading problem ...

Reply
Thread Tools

Dynamic class loading problem ...

 
 
biomechanism
Guest
Posts: n/a
 
      05-22-2004
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]

 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      05-22-2004
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?

 
Reply With Quote
 
 
 
 
biomechanism
Guest
Posts: n/a
 
      05-22-2004
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]

 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      05-22-2004
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.

 
Reply With Quote
 
biomechanism
Guest
Posts: n/a
 
      05-23-2004
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]

 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      05-23-2004
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...

 
Reply With Quote
 
biomechanism
Guest
Posts: n/a
 
      05-24-2004
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]


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic loading of .class files and deserializing of object Goofball Java 1 06-11-2007 06:27 PM
Scatter/Gather in Java or Javascript & html (Dynamic class loading?) Richard Maher Java 16 02-20-2007 04:26 PM
cocoon + dynamic class loading Denis Nikiforov Java 1 03-18-2006 05:42 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Dynamic Class Loading Aguilar, James C++ 17 07-16-2004 03:22 PM



Advertisments