java - Issue when dynamically setting properties using reflection -
i have task object properties need populated data received via json web service. property names mapped json keys. using following code in attempt populate object app crashes when hits line:
while(looper.hasnext()){ string key = looper.next(); string val = json.get(key).tostring(); user.getclass().getdeclaredfield(key).set(user, val); // crash }
the object called user. have verified key variable match property in user object. ideas on how fix this? thanks!
you should set field accessible
field field = user.getclass().getdeclaredfield(key); if (field != null) { field.setaccessible(true); field.set(user, val); }
Comments
Post a Comment