M.Gwozdz
M.Gwozdz

Reputation: 129

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.

Upvotes: 1

Views: 29290

Answers (6)

Pedro P. Silva
Pedro P. Silva

Reputation: 1

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.

Upvotes: 0

M.Gwozdz
M.Gwozdz

Reputation: 129

Ok, here is solution. In fact object is an array, getDeclaredFields() return empty table, in documentation we can read:

If this Class object represents an array type, a primitive type, or void, then this method returns an array of length 0.

So in this situation it is useless. All we have to do is iterate over this object this way:

for(Object o: displayResponseList) {
    for(int i = 0; i < 7; i++) {
        System.out.println(((Object[])o)[i].toString());
    }
    System.out.println("...............");
}

Hope this will help someone in future.

Upvotes: 3

Flikk
Flikk

Reputation: 540

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

Upvotes: 0

karim mohsen
karim mohsen

Reputation: 2254

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);
}

Upvotes: 1

Daniel Mart&#237;n
Daniel Mart&#237;n

Reputation: 117

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

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

Upvotes: 2

Yoav Gur
Yoav Gur

Reputation: 1386

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);

Upvotes: 2

Related Questions