mardi 24 mai 2016

Why do I get NoSuchMethodException?

I keep getting NoSuchMethodException in this line:

float modif = (float)sc.affectingObject.getClass().getMethod(sc.affectingMethodName, (Class<?>[])null).invoke(sc.affectingObject, (Object[])null);

where sc is an instance of class SubChange:

class SubChange implements Serializable {
    String changeType;
    Serializable affectingObject;
    String affectingFieldName;
    String affectingMethodName;

    public SubChange(String chanType, Serializable affingObj, String affingFM) {
        try{
            changeType = chanType;
            affectingObject = affingObj;
            //deciding whether affingFM provides a field name or a method name
            for (Field f : affingObj.getClass().getDeclaredFields()) {
                if (f.getName().equals(affingFM) == true) {
                    affectingFieldName = affingFM;
                    break;
                }
            }
            if (affectingFieldName == null) {
                affectingMethodName = affingFM;
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    //other class methods
}

whose instance has been initialized like this:

new SubChange("first", physio, "calcTotalFatigue")

where physio is an instance of inner class belonging to class Hm, while the SubChange constructor is being called from another inner class of the same Hm class.

Needless to say that method calcTotalFatigue() of physio exists.

Can anyone, please, suggest what I am doing wrong?





Aucun commentaire:

Enregistrer un commentaire