Class's newInstance() method returning null

Discussion in 'Java' started by Rick, Jun 25, 2004.

  1. Rick

    Rick Guest

    I am using a custom ClassLoader to load a class. The class loads fine,
    but the instantiation with Class's newInstance() method returns a
    null.

    Code snippet:
    TestClassLoader loader = new TestClassLoader(dirName);
    System.out.println("\n\ntest = " + test + "\n");
    Class c = Class.forName("tests.Kiv7Test",true,loader);
    Object t = c.newInstance(); //OBJECT t is NULL!!!
    System.out.println("Class loaded: " + c.getName () + " Object:" + t +
    " isInstance: " + c.isInstance (t));

    Output:
    test = tests.Kiv7Test

    default Kiv7Test
    Class loaded: tests.Kiv7Test Object:null(tests.Kiv7Test) isInstance:
    true


    The "default Kiv7Test" output is a simple println() in the Kiv7Test
    no-arg constructor, so we know the Kiv7Test was loaded, instantiated,
    and ran properly.

    Any advice on how to overcome this situation (and an explanation of
    what I'm doing wrong) would be appreciated.

    TIA!

    -Rick
    Rick, Jun 25, 2004
    #1
    1. Advertising

  2. Rick wrote:
    >
    > I am using a custom ClassLoader to load a class. The class loads fine,
    > but the instantiation with Class's newInstance() method returns a
    > null.
    >
    > Code snippet:
    > TestClassLoader loader = new TestClassLoader(dirName);
    > System.out.println("\n\ntest = " + test + "\n");
    > Class c = Class.forName("tests.Kiv7Test",true,loader);
    > Object t = c.newInstance(); //OBJECT t is NULL!!!
    > System.out.println("Class loaded: " + c.getName () + " Object:" + t +
    > " isInstance: " + c.isInstance (t));
    >
    > Output:
    > test = tests.Kiv7Test
    >
    > default Kiv7Test
    > Class loaded: tests.Kiv7Test Object:null(tests.Kiv7Test) isInstance:
    > true
    >
    > The "default Kiv7Test" output is a simple println() in the Kiv7Test
    > no-arg constructor, so we know the Kiv7Test was loaded, instantiated,
    > and ran properly.
    >
    > Any advice on how to overcome this situation (and an explanation of
    > what I'm doing wrong) would be appreciated.


    If your code snippet is correct, t is not null. Its toString() is returning
    "null(tests.Kiv7Test)" and c.isInstance is returning true.

    To double check, add this to your println: + "t-is-null=" + (t==null)

    --
    Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
    Lee Fesperman, Jun 25, 2004
    #2
    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. Miguel De Anda

    ClassLoader or Class.newInstance

    Miguel De Anda, Jul 14, 2003, in forum: Java
    Replies:
    1
    Views:
    820
    John C. Bollinger
    Jul 15, 2003
  2. Per Magnus L?vold
    Replies:
    0
    Views:
    1,362
    Per Magnus L?vold
    Nov 15, 2004
  3. Aryeh M. Friedman
    Replies:
    3
    Views:
    1,467
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Oct 15, 2007
  4. Abu Yahya
    Replies:
    43
    Views:
    3,014
    Michael Wojcik
    Jun 23, 2011
  5. femto femto
    Replies:
    3
    Views:
    164
    Trans
    Aug 17, 2006
Loading...

Share This Page