'Getting values from object in java

How I can get those values from this object? I was trying to getFields, getDeclaredFields etc. but everything is empty.

enter image description here

The problem is that Field[] myField = o.getClass().getDeclaredFields(); always return an empty array. I am getting those values from database this way:

Query reqDisplayResponse = em.createNativeQuery("Select * FROM pxds_displayResponse");
List<Object> displayResponseList = reqDisplayResponse.getResultList();

And I want to print those values:

for(Object o: displayResponseList) {
    for(Field field: o.getClass().getDeclaredFields()) {
        log.info(field.getName());
    }
}

Unfortunately log.info is unreachable.



Solution 1:[1]

Try to display the object 'o' like an array:

for(int index = 0 ; index < 10 ; index++){
     Log.info(String.valueOf(o[index]));
} 

Solution 2:[2]

You should use getDeclaredField, and then use get on it, passing the object as parameter. Like this:

Field myField = object.getClass().getDeclaredField("_myField");
myField.setAccessible(true);
return (Integer) myField.get(object);

Solution 3:[3]

I think those fields you are trying to access are private

So in order to access private fields you have to:-

for (Field f : object.getClass().getDeclaredFields()) {
    f.setAccessible(true);
    Object o;
    try {
        o = f.get(object);
    } catch (Exception e) {
        o = e;
    }
    System.out.println(f.getGenericType() + " " + f.getName() + " = " + o);
}

Solution 4:[4]

This is an ID given by the Eclipse debugger, not by Java. You cannot access it.
There is System.identityHashCode(Object) to get the Object identity. (not the same ID)
If you want an ID like the one shown in the Eclipse debugger, you'd have to allocate them yourself.

Here is some general direction how you could do something like that:
Elegant way to assign object id in Java

Solution 5:[5]

Gwozdz, I think I understand your question. If I understood correctly, you are having problemas to access the value from a list of objects, in your image code example I'm seeing that you are using List. Try to use List<Object[]> and then use a foreach to access every value of your matrix.

List<Object[]> displayResponseList = reqDisplayReponse.getResultList();
foreach(.....){
   foreach(.....){
   [manipulate you object value here]
   }
}

Just for your information: Matrix is a list of lists. In that case a list of array.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Martín
Solution 2 Yoav Gur
Solution 3 karim mohsen
Solution 4 Community
Solution 5 Pedro P. Silva