Reputation: 987
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.
Upvotes: 0
Views: 75
Reputation: 761
Even I analyzed the same. Value of your array in the object is null
import java.lang.reflect.Array; import java.lang.reflect.Field;
class Shape {
int numberOfSides;
int[] arry = new int[100];
public int getNumberOfSides() {
return numberOfSides;
}
public void setNumberOfSides(int numberOfSides) {
this.numberOfSides = numberOfSides;
}
}
public class App {
public static void main(String[] args) throws ClassNotFoundException,
IllegalArgumentException, IllegalAccessException {
printDetails(new Shape());
}
public static void printDetails(Object o) throws IllegalArgumentException,
IllegalAccessException {
Field[] fields = o.getClass().getDeclaredFields();
Field field = fields[1];
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());
}
}
}
}
Run the code as is it will work. After that Modify class Shape to
class Shape{
int numberOfSides;
int[] arry;
public int getNumberOfSides() {
return numberOfSides;
}
public void setNumberOfSides(int numberOfSides) {
this.numberOfSides = numberOfSides;
}
}
You will have your exception. Hope this helps
Upvotes: 1
Reputation: 28294
My guess is that the field you are trying to access in your object is null. Here is a sample of your own code that works for an array that is not null and reproduces your results with an array that is null.
Oh, and I modified your method signature to take the Field parameter. Just a heads up incase you are going to test it:
public static void printDetails(Object o, Field field) throws IllegalArgumentException, IllegalAccessException {
And the code:
private static class Test {
public int intField;
public int[] nullIntArrayField;
public int[] notNullIntArrayField = new int[]{1,2,3};
}
public static void main(String[] args){
test();
}
private static void test() {
try {
Test test = new Test();
Class testClass = test.getClass();
Field intField = testClass.getField("intField");
printDetails(test, intField);
//works!!!
intField = testClass.getField("notNullIntArrayField");
printDetails(test, intField);
//doesnt work!!!
intField = testClass.getField("intArrayField");
printDetails(test, intField);
} catch (NoSuchFieldException ex) {
Logger.getLogger(Object.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(Object.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(Object.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Object.class.getName()).log(Level.SEVERE, null, ex);
}
}
Upvotes: 1