mercredi 26 septembre 2018

How do I handle Java NoSuchFieldException?

I am trying to loop through request body keys and set values of an object when the key and an object field correlate like so:

User user = new User((String) super.session.get("id"));
Class<?> c = user.getClass();
for(String key : super.body.keySet()){
    Field f = c.getField(key);
    if(f != null){
        if(key.equals("avatar")){
            uploadFile();
        }
        f.set(user, super.body.get(key));
    }
}
user.update();

However, I keep getting the NoSuchFieldException and my code stops working. How can I deal with the possibility of a field not existing?





Aucun commentaire:

Enregistrer un commentaire