Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Field.get(Object obj) now returning null with Generics (Java 5.0) ??

843793Jan 30 2008 — edited Jan 31 2008
Hello,

I'm currently using Java 5.0 (especially for the Generics part) on a new Java/J2EE project, but having a strange issue with code working previously in a Java 1.4 project.

Below is an overriding of the toString() method provided by the Object class which allow me to view nicely in debug (dev. mode) the contents of my Transfer Objects (all the TO's must extend this ATO abstract class).

Previously this code displayed me something like:
[field1 => value1, field2 => value2]
... for a TO (sort of "Javabean") having e.g. two String fields with values initialized to "value1" (resp. "value2").
But unfortunately, this does (or seems) not to work anymore, having such display :
[field1 => null, field2 => null]
I tried to debug, and the problem is that the call
fieldValue = field.get(this);
returns null while it should returns the actual value of the field.

I thing it it strongly related to Generics, but could not at the moment found how/why it does not work.
May someone help...? Thanks.

public abstract class ATO {

    // Reflection for field value display
    public String toString() {
        StringBuffer sb = new StringBuffer("[");
        MessageFormat mf = new MessageFormat("{0} => {1}, ");
        Field[] fields = this.getClass().getDeclaredFields();

        for (int i = 0; i < fields.length; i++) {
            Field field = (Field) fields;
String fieldName = field.getName();
Object fieldValue = null;
try {
fieldValue = field.get(this);
} catch (IllegalArgumentException e) {

} catch (IllegalAccessException e) {
}
mf.format(new Object[] { fieldName, fieldValue }, sb, null);
}
if (sb.length() > 1) {
sb.setLength(sb.length() - 2);
}
sb.append("]");

return sb.toString();
}

}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 28 2008
Added on Jan 30 2008
4 comments
450 views