• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

access field in unknown class using reflection

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting an IllegalArgumentException with the message "object is not an instance of declaring class" when I try to get a value from a field in the following class:

The parent object is a reference to the declaring object and is passed to the FieldValidator in the following statement in the parent class:

I can't understand how the parent object is not seen to be an instance of the declaring class. Can anyone help me understand what I need to do?
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the line:

should be
 
Mark Frasertoo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Passing parent instead of parentClass gives a NoSuchFieldException.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to look at (and show us) the stack traces from the exceptions as they give you the line number the exception occurred on.
I don't think the same line is throwing that exception because the API docs for the method get() don't show a NoSuchFieldException as being thrown.
 
Mark Frasertoo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have a stack trace showing the line number but the documentation says the NoSuchFieldException is thrown by the getField(fieldName) call.

It feels like I need to cast to the class of the parent object but the idea of the method is to not need it, and that's what reflection is supposed to allow. Am I wrong about that?

This is what I have from the Tomcat logs:
 
Mark Frasertoo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should mention that I had a problem with persistent sessions and have disabled them. Now the NoSuchFieldException occurs with the original code.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I said to change "parentClass" to "parent" in the get() method not in the getField() method. Using parentClass in getField() is correct but when you call the Field object's get() method you need to pass in the object who's field you want and not the class type.
 
Mark Frasertoo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tony, but the NoSuchFieldException is before that line.

 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case the Class doesn't contain the field you are trying to get a Field object for.
I suggest you print out the Class type and the fieldName values and make sure they are correct ie check that the class identified by the Class type contains a a field with the name in fieldName (and remember to check case as well as spelling).
 
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Frasertoo wrote:Thanks Tony, but the NoSuchFieldException is before that line.



The fact you are calling setAccessible(true) implies to me that the field you're attempting to access is not public.

The getField() method only returns public fields. If you want to get any non-public fields you need to use getDeclaredField(...)
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike. J. Thompson wrote:The fact you are calling setAccessible(true) implies to me that the field you're attempting to access is not public.

The getField() method only returns public fields. If you want to get any non-public fields you need to use getDeclaredField(...)


Well spotted Mike.
 
reply
    Bookmark Topic Watch Topic
  • New Topic