What's the problem in this code

import java.lang.reflect.*;

import java.awt.*;

class ABC

{

public Integer i;

ABC()

{

}

public void setInt(Integer t)

{

i = t;

}

}

public class SampleName {

public static void main(String[] args)

{

ABC g1 = new ABC();

g1.setInt(new Integer(10));

printFieldNames(g1);

}

static void printFieldNames(Object o) {

Class c = o.getClass();

Field[] publicFields = c.getDeclaredFields();

for (int i = 0; i < publicFields.length; i++)

{

try {

Object ref = publicFields.get(c);

System.out.println(" ref.toString() : " + ref.toString());

}catch(Exception e)

{

e.printStackTrace();

}

}

}

What is the problem with this code,at run time Iam getting this exception

java.lang.IllegalArgumentException: object is not an instance of declaring class

How can we get the value of field of an object

[1027 byte] By [vanivenkat99] at [2007-9-26 2:41:46]
# 1
> Object ref = publicFields[ i ].get(c);change this to:Object ref = publicFields[ i ].get(o);
artntek at 2007-6-29 10:17:51 > top of Java-index,Archived Forums,Java Programming...
# 2
Now it got this exceptionjava.lang.IllegalAccessExceptionat java.lang.reflect.Field.get(Native Method)
vanivenkat99 at 2007-6-29 10:17:51 > top of Java-index,Archived Forums,Java Programming...
# 3

> Now it got this exception

>

> java.lang.IllegalAccessException

> at java.lang.reflect.Field.get(Native Method)

That's strange - I didn't! ;-)

Are you running *exactly* the same code as the code you posted (except for the one line I said to change)?

artntek at 2007-6-29 10:17:51 > top of Java-index,Archived Forums,Java Programming...
# 4
If I run exactly the same code, it is working fine.I have tried changingpublic Integer i; toprivate Integer i; that time it is showing that exception.
vanivenkat99 at 2007-6-29 10:17:51 > top of Java-index,Archived Forums,Java Programming...
# 5
...I assume you know why that doesn't work, right?
artntek at 2007-6-29 10:17:51 > top of Java-index,Archived Forums,Java Programming...
# 6
it can't access the values of private fields , is that right!
vanivenkat99 at 2007-6-29 10:17:51 > top of Java-index,Archived Forums,Java Programming...
# 7
absolutely!:-)
artntek at 2007-6-29 10:17:51 > top of Java-index,Archived Forums,Java Programming...
# 8
Thank you very much
vanivenkat99 at 2007-6-29 10:17:51 > top of Java-index,Archived Forums,Java Programming...