• 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

java.lang.Reflection throwing error when using (String)field.get(new String());

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a class in Categories.java with variable definitions, which I want to read with Reflection:



and I want to read the value of the String with the following program called Reflect.java :



and this is the error message:



I am using this type conversion because there is no getString() or getStr() method in java.lang.reflect.Field - what else can I use? I've been googling for this, but found no solution now that some hours have passed.
Thank you for helping me with this!
Oliver
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What else could you use? Well, there is the documentation for the Field.get() method. Which says

Returns the value of the field represented by this Field, on the specified object.


You have made a bad choice for "the specified object". Admittedly the documentation isn't all that clear, but it still doesn't support that choice. Read
it (again?) more carefully.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are calling field.get with the wrong argument.

Some other remarks:

It is almost never necessary to create new String objects explicitly. Class String is immutable. Instead of writing things like these:

Just write this, which has the same effect, except that it does not unnecessarily create a new String object:


Something similar for Integer and Long objects, instead of writing this:

Just write this, making use of autoboxing (which can avoid unnecessarily creating new objects):

 
Oliver Bachmann
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul, thank you very much for your answer.

This is working:

when setting the correct class name as parameter.

The original description of the problem was too short. In the original sourcecode (not the example I posted here) I only had an Object available, and not the class Categories itself. Besides that, I also passed the wrong object instance to getField, not really knowing that it had to be an instance of the class Categories.
I figured out how it works, using an Object instead of Categories itself:



The output is:



Jesper, thank you for the good, short answer and the tips regarding the initialization of my variables

Thank you,
Oliver
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oliver Bachmann wrote:


Don't do that, please. You should get the String class literal and compare directly:
Likewise for Integer, Long, etc. Remember, you can get a class literal for each class by just saying X.class, with X being the class name. You can even do that for arrays, for instance String[].class is the class for String arrays, and String[][].class is the class for arrays of arrays of Strings.
reply
    Bookmark Topic Watch Topic
  • New Topic