java - Getting Error While Using Reflection to get Field Data -
i trying fetch field name field value using reflection.
i passing dynamic classes per operation needed.
i have made method fetch field name , value, getting field name not getting field value. when using following code gives me error java.lang.illegalaccessexception stating can not access private member of class.
following updated code :
public string serializecommand(icommand command){      stringbuilder command_text = new stringbuilder();     field [] f = command.getclass().getdeclaredfields();     for(field field : f){         field.setaccessible(true);         command_text.append(field.getname() + ",");         try {             system.out.println(field.get(command.getclass()));         } catch (illegalaccessexception e) {             e.printstacktrace();         }     }     return command_text.tostring(); }   here icommand class name it, suppose if operation add add class passed.
any idea solve problem.
please try code.
public string serializecommand(icommand command){      stringbuilder command_text = new stringbuilder();     field [] f = command.getclass().getdeclaredfields();     try{     for(field field : f){         field.setaccessible(true);         command_text.append(field.getname() + ",");         system.out.println("value :: " + field.get(command));     }     }catch(illegalargumentexception e){         e.printstacktrace();     } catch (illegalaccessexception e) {         e.printstacktrace();     }     return command_text.tostring(); }      
Comments
Post a Comment