Instanciate an Object by introspection and set it a null value

Hello,

I'm actually using introspection to instanciate some Objects.

I'd like to set them to null, but it doesn't work...

Object obj = Class.forName("java.lang.String").newInstance();

obj =null;

System.out.println("obj Class : " + obj.getClass());// Doesn't work : NullPointerException

Object obj2 = Class.forName("java.lang.String").newInstance();

System.out.println("obj2 Class :" + obj2.getClass());// Work and displays java.lang.String

So, is it possible to make this by introspection ?

String s =null;

Doesn someone has an idea ?

Thanks in advance.

_ _ _

bgOnline

[890 byte] By [bgOnlinea] at [2007-10-3 3:51:06]
# 1
You can't set an object to a null value. You can only ever set references. What exactly do you want to achieve?You can set fields to null using reflection.You can create object instances using reflection.You can't "set an object to null".
JoachimSauera at 2007-7-14 21:48:42 > top of Java-index,Core,Core APIs...
# 2

Reflection, or anything you do before assigning null to "obj", doesn't have anything to do with the fact that you get a NPE when calling a method on it.

Object obj;

//do whatever

obj = null;

System.out.println("obj Class : " + obj.getClass());

// NullPointerException, obviously, since obj == null

> I'm actually using introspection to instanciate some

> Objects.

You actually instantiate a class...

> I'd like to set them to null, but it doesn't work...

... and set references to null (not objects).

Lokoa at 2007-7-14 21:48:42 > top of Java-index,Core,Core APIs...
# 3

If you type this in Java, you won't have any error :

String s = null;

System.out.println("s Class : " + s.getClass());

But if you do that, you will have a NullPointerException :

Object o = Class.forName("java.lang.String").newInstance();

o = null;

System.out.println("o Class : " + o.getClass()); // NullPointerException as the Object o has been assigned to null

Do you understand what I mean ?

Anyway, I solved my problem in another way : I check if the value is null, in which case I call the write method by introspection without creating a new instance of the Object.

Thanks for your help.

__ _ _ _

bgOnline

bgOnlinea at 2007-7-14 21:48:42 > top of Java-index,Core,Core APIs...
# 4

> If you type this in Java, you won't have any error :

> > String s = null;

> System.out.println("s Class : " + s.getClass());

>

1) You're not setting an object to null. Objects are never null. Only references are.

2) You will get a NullPointerException when you run that code.

jverda at 2007-7-14 21:48:42 > top of Java-index,Core,Core APIs...
# 5

> If you type this in Java, you won't have any error :

> > String s = null;

> System.out.println("s Class : " + s.getClass());

>

If you actually try this, by compiling and running, you do get a NullPointerException.

What output do you see then?

> Do you understand what I mean ?

I understand you have either made your claim without even trying it your own code, or you must be using a very weird Java version where calling a method on a null reference doesn't throw a NullPointerException.

Lokoa at 2007-7-14 21:48:42 > top of Java-index,Core,Core APIs...
# 6

> I understand you have either made your claim without

> even trying it your own code, or you must be using a

> very weird Java version where calling a method on a

> null reference doesn't throw a NullPointerException.

Well, it would work if getClass were static. :-)

(Although, I guess you wouldn't really be calling the method "on a null reference" then.)

jverda at 2007-7-14 21:48:42 > top of Java-index,Core,Core APIs...
# 7
> Well, it would work if getClass were static. :-)That would seriously fudge up reflection :-). But good point, I do keep forgetting you can call a static method "on" a reference because, well, who would do such a thing, I mean really?
Lokoa at 2007-7-14 21:48:42 > top of Java-index,Core,Core APIs...