0
// getting entity
Object obj= items.get(i).returnEntity();

// finding fields
for(Field field : obj.getClass().getFields()){          
    if(field.getType().isAssignableFrom(String.class)){             
     Log.v(TAG, field.getName());  // it is working i can get field name

    Object value = field.get(obj); // value always `null` ???

How can I get the values of fields ?

I looked at below answer but I don't understand why I'm getting null ?

How to get the fields in an Object via reflection?

Reflection generic get field value

Community
  • 1
  • 1
Talha
  • 12,673
  • 5
  • 49
  • 68
  • 3
    Are you sure that a) the fields are actually not null, and b) your program isn't throwing an exception at the call to `field.get`? – Paul Bellora Jun 03 '13 at 15:44
  • 2
    How does the `returnEntity` intialize the returned `Object`? It is `null` probrably because the field is not initialized yet. Getting value of `Field.getName()` doesn't mean that the field has value. – Genzer Jun 03 '13 at 15:44
  • Well, if the values are not null, may be you need to set the modifier as accessible by using: field.setAccessible(true); Also, can you paste the stack trace here as well? – SSC Jun 03 '13 at 15:53
  • when i debug the code i can see that entity is not null. – Talha Jun 03 '13 at 16:31
  • it is my mistake, i looked at the code again and it is working. There are lots of null valu in return object and i thought there are a problem. But when i looked all field value, there are some fields is not null. Sorry for wasting your time ! – Talha Jun 03 '13 at 16:37

3 Answers3

3

I'm guessing, but the name returnEntity suggests that you might be dealing with Hibernate (or JPA) entities or similar.

Those might load their fields only lazily when accesst through getters. If you access them through the fields they still have all null values.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • it is my mistake, i looked at the code again and it is working. There are lots of null valu in return object and i thought there are a problem. But when i looked all field value, there are some fields is not null. Sorry for wasting your time ! – Talha Jun 03 '13 at 16:37
2

The field might not have been initialized. For ex:

class TestClass
{
    Object o;
}

public class Test
{
    public static void main(String[] args)
    {
        TestClass t = new TestClass();
        Class c = t.getClass();
        Field f = c.getDeclaredField("o");
        Object obj = f.get(t);  // will be null
    }
}

The obj will be null in this case. But if the Object o in TestClass had been initialized, the value of o would not be null:

class TestClass
{
    Object o = new Object();
}

This would not be null:

Object obj = f.get(t);  // will not be null
Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48
  • it is my mistake, i looked at the code again and it is working. There are lots of null valu in return object and i thought there are a problem. But when i looked all field value, there are some fields is not null. Sorry for wasting your time ! – Talha Jun 03 '13 at 16:37
1

one possibility is that you encounter of the byte-code manipulation done by hibernate on the entities. hibernate entities are lazy loaded (by default) . hibernate sticks hooks into the getter/setter methods to load the actual values when you call one of those methods.

You should always use the getters/setters in a hibernate object, not direct field access.

Uri Lukach
  • 1,093
  • 1
  • 14
  • 28
  • it is my mistake, i looked at the code again and it is working. There are lots of null valu in return object and i thought there are a problem. But when i looked all field value, there are some fields is not null. Sorry for wasting your time ! – Talha Jun 03 '13 at 16:44