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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -