Field field = (obj.getClass().getDeclaredClasses()[0])
.getDeclaredField("flowerName");//1
field.setAccessible(true);//2
field.set(null, "Rose");//3
In the above code line number 3, I'm getting the NullPointerException
The class structure that is passed is shown below
public class A{
public static class B{
protected String flowerName;
}
}
I want to set the value to this flowerName variable at run time using java reflection. But it is throwing NullPointerException.
I referred in some places, wherein it has been specified that when you are trying to access the instance variable and setting null in the set method like set(null, "Rose"), it will throw null pointer exception. So then how to set the value of flowerName in the static nexted class using java reflection.