Thank you Stephen. That nailed it. The
problem was not so obvious to me.
Although the API doc for
Class.newInstance(), does not state it explicitly, you must code a no-args
constructor into the class to instantiate it using newInstance(). Replacing my 1
arg constructor with a no-arg constructor and a setter method fixed my
problem.
The behaviour of of Class.newInstance() and
Class.create(object), which implements programmatic
instantiation using constructors with arguments, is documented in the Java Tutorial. RYFM strikes again.
instantiation using constructors with arguments, is documented in the Java Tutorial. RYFM strikes again.
----- Original Message -----
From: "Stephen Davidson" <[EMAIL PROTECTED]>
To: "Orion-Interest" <[EMAIL PROTECTED]>
Sent: Tuesday, February 05, 2002 9:08
AM
Subject: Re: Class.newInstance() Fails with
InstantiationException.
> hj (in this case, com.wynnon.appletChannel.test.EchoingPort) does have a no-arg constructor, right?
> In other words,
> Object obj = new EchoingPort()
> is legal?
> </Obvious Question>
>
> -Steve
>
> Bill Winspur wrote:
>
> > The echoing port never gets instantiated, the attempt to instantiate, by
> > invoking Class.newInstance(), throws an InstantiationException. However,
> > if you are asking what is the function of EchoingPort? ... it is a test
> > stub for for the session side of an http tunneling package to support
> > applet/client access to ejb's via http.
> >
> >
> >
> > The relevant code follows:--------------------------------------------
> >
> >
> >
> > /**
> > * Instantiates the session port if not yet done
> > */
> > private void instantiateIt(String className) throws Exception {
> >
> > Class hj = null;
> > try {
> > hj = Class.forName(className);
> > System.out.println(
> > "class: " + hj +
> > "\n. isInterface: " + hj.isInterface() +
> > "\n. isArray: " + hj.isArray() +
> > "\n. isPrimitive: " + hj.isPrimitive() +
> > "\n. isAbstract: " + Modifier.isAbstract(hj.getModifiers()) +
> > "\n. isFinal: " + Modifier.isFinal(hj.getModifiers()) +
> > "\n. isNative: " + Modifier.isNative(hj.getModifiers()) +
> > "\n. isPrivate: " + Modifier.isPrivate(hj.getModifiers()) +
> > "\n. isProtected: " + Modifier.isProtected(hj.getModifiers()) +
> > "\n. isPublic: " + Modifier.isPublic(hj.getModifiers()) +
> > "\n. isStatic: " + Modifier.isStatic(hj.getModifiers()) +
> > "\n. isStrict: " + Modifier.isStrict(hj.getModifiers()) +
> > "\n. isStrict: " + Modifier.isStrict(hj.getModifiers()) +
> > "\n. isTransient: " + Modifier.isTransient(hj.getModifiers()) +
> > "\n. isVolatile: " + Modifier.isVolatile(hj.getModifiers())
> > );
> > try {
> > Object oj = hj.newInstance();
> > trace("obj: "+ oj);
> > trace("Instantiated: " + hj);
> > } catch (InstantiationException e) {
> > throw new Exception(
> > "Cant instantiate a session object: " + e);
> > } catch (IllegalAccessException e) {
> > throw new Exception(
> > "Cant instantiate a session object" + e);
> > }
> > } catch (Exception ex) {
> > throw new Exception(
> > "Failed to instantiate className: " + className +
> > "\n" + ex.toString() );
> > }
> > }
> >
> >
> > private void trace(String s) { System.out.println(s); }
> >
> >
> >
>
> [snip to save bandwidth]
>
> --
> Stephen Davidson
> Java Consultant
> Delphi Consultants, LLC
> http://www.delphis.com
> Phone: 214-696-6224 x208
>
>
>
