0

I am trying to obtain a field's value via reflection:

 for (Field field : entity.getClass().getDeclaredFields()) { log.info("[Field name] {} ", field); try { log.info("[Field value] {} ", field.get(entity)); } catch (Exception e) { log.error(e.getMessage()); } } } 

But I have this error:

 cannot access a member of class com.model.Dog with modifiers "private" 
0

1 Answer 1

2

You forgot to make the field accessible like so:

for (Field field : entity.getClass().getDeclaredFields()) { log.info("[Field name] {} ", field); try { field.setAccessible(true); log.info("[Field value] {} ", field.get(entity)); } catch (Exception e) { log.error(e.getMessage()); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.