1

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.

M.Gwozdz
  • 129
  • 1
  • 2
  • 10
  • 1
    Please show a [mcve] which demonstrates what you're trying to do and what goes wrong, rather than just a debugger screenshot. – Jon Skeet Feb 24 '16 at 07:29
  • Well I am just trying to print those 7 values from Object o. I understand what you mean by examples, but in fact there is no more to show, It's not question about error problem, it's about how to do something – M.Gwozdz Feb 24 '16 at 07:34
  • 1
    Well if you tried getDeclaredFields and it didn't work, then that sounds like a problem. But we can't know what the problem was if you don't show how you tried to use that method. – yshavit Feb 24 '16 at 07:36
  • Yes, there *is* more to show... you should be able to post a short but complete example of this failing to do what you expect. You don't even need the list - just a class with fields you want to get at, and a demonstration of failing to get at them with reflection. – Jon Skeet Feb 24 '16 at 07:36
  • Field[] myField = o.getClass().getDeclaredFields(); This line of code returns an empty array, not null just empty. Thats why for doesn't work. – M.Gwozdz Feb 24 '16 at 07:43
  • 2
    @user2145530: So you should be able to show that in a [mcve], shouldn't you? (Although it's not clear why you're trying to get the fields at all, to be honest. What are you trying to *do* with them?) – Jon Skeet Feb 24 '16 at 09:55

6 Answers6

3

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.

M.Gwozdz
  • 129
  • 1
  • 2
  • 10
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);
Yoav Gur
  • 1,366
  • 9
  • 15
2

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

for(int index = 0 ; index < 10 ; index++){
     Log.info(String.valueOf(o[index]));
} 
  • Cannot do that because o is an object not array: The type of the expression must be an array type but it resolved to Object – M.Gwozdz Feb 24 '16 at 07:57
1

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);
}
karim mohsen
  • 2,164
  • 1
  • 15
  • 19
0

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

Community
  • 1
  • 1
Flikk
  • 520
  • 3
  • 10
0

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.