Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not being successful with Reflection

 
Yogendra Joshi
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks ,

I have been trying my hands with reflection today.. and bored with this error

What i am trying to acheive is , I have a class in which i get the name of other class using Class.forName("ReflectionTest") like this :

Class Yogs=Class.forName("ReflectionTest");

then i make an instance of this class by referring to Object class like this :

Object NewInst = Yogs.newInstance();

Then in the try section , I try to Locate a String field named hello using Field class , but when i run the code , it gives the above NoSuchFieldException , Can all of you experts highlight , where i am going wrong , below are the 2 java files which i have used for the above.






Please help in this new learning of mine.
Thanks in advance.

Yogendra N Joshi
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code works fine except for the fact that your class ReflectionTest does not compile as in the for loop you have not declared the variable i.
After fixing the above problem, the program runs fine.
Probably you have an earlier version of the class ReflectionTest that does not have the field Hello.
 
Daniel Chemko
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java fields are case sensitive:

private String field = "lowerCase";
private String Field = "firstUpperCase";
private String FIELD = "allUpperCase";

getClass().getField("field").get(this) == "lowerCase"
getClass().getField("Field").get(this) == "firstUpperCase"
getClass().getField("FIELD").get(this) == "allUpperCase"
 
Yogendra Joshi
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitesh / Daniel ,

A great thanks for your wonderful help. I have been able to get the type now. It works fine.

Thanks a lot

Yogendra N Joshi.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic