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.

6
  • 1
    Please show a minimal reproducible example which demonstrates what you're trying to do and what goes wrong, rather than just a debugger screenshot. Commented Feb 24, 2016 at 7: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 Commented Feb 24, 2016 at 7: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. Commented Feb 24, 2016 at 7: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. Commented Feb 24, 2016 at 7:36
  • 2
    @user2145530: So you should be able to show that in a minimal reproducible example, 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?) Commented Feb 24, 2016 at 9:55

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

2

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

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

1 Comment

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

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.