Function takes an Object and stores all the non-static field in an array of Field type. 'for' with every non-static field recorded. If the non-static field is of primitive type then the 'if' part goes fine. But if the field is an array then the else part produces problem. The line producing problem is highlighted. Is this the correct way to access an array? Code of this part of my program is as follows.
public static void printDetails(Object o)
{
// ...
// field is a non-static field of the Class of Object 'o'
String fieldName = field.getType().getName();
if(fieldName.equals("int") || fieldName.equals("float") || fieldName.equals("java.lang.Boolean") || fieldName.equals("long") ||
fieldName.equals("short") || fieldName.equals("char") || fieldName.equals("java.lang.String"))
{
Object sendObj = o, recvObj = null;
recvObj = field.get(sendObj);
System.out.println("\t " + recvObj.toString()); // Ok for primitive data type case
}
else if(field.getType().isArray())
{
Object sendObj = o;
Object recvArray = field.get(sendObj); // returns 'null', array expected
int length = Array.getLength(recvArray);
for(int i=0; i<length; ++i)
{
Object element = Array.get(recvArray, i);
System.out.println(element.toString());
}
}
}
Any help would be greatly appreciated.