samedi 7 février 2015

Java reflection - how to call setter method?

I'm trying to call set method of some property that I got in my object, my code goes like this:



String[] fieldsStringName = (((CacheObject)currentObject).getFieldsToString(false)).split(", ");
String methodName = "";

for (int i = 0; i < objectInputArr.length; i++) {
methodName = "set" + fieldsStringName[i];

Method methodSetProperty = currentObject.getClass().getMethod(methodName); <<----error goes here
methodSetProperty.invoke(currentObject, objectInputArr[i]);
}


The error that i'm getting is:



Exception in thread "main" java.lang.NoSuchMethodException: model.Book.setPagesAmount()
at java.lang.Class.getMethod(Unknown Source)
at CahceSystem.createNewObject(CahceSystem.java:84)
at CahceSystem.main(CahceSystem.java:50)


The requested setter method is well written in the class and also in it's super class (inherited propeties only)


Any suggestions how to solve this issue?






Aucun commentaire:

Enregistrer un commentaire