Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Class's newInstance() method returning null

Reply
Thread Tools

Class's newInstance() method returning null

 
 
Rick
Guest
Posts: n/a
 
      06-25-2004
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
 
Reply With Quote
 
 
 
 
Lee Fesperman
Guest
Posts: n/a
 
      06-25-2004
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)
 
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
file reader returning null when file is not null content jason Java 13 05-14-2010 01:50 AM
Cannot invoke Call with null namespace URI for method null raviupasi@gmail.com Java 0 05-12-2006 01:34 PM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
OnEditCommand - .FindControl Returning Null George Durzi ASP .Net 0 07-14-2003 09:54 PM



Advertisments