0

I am new in Java and have a task to write some application. Faced one problem which can not pass :(

The issue is to update an array element through reflection (app selecting public array to update dinamicaly depending on string app reading from file):

First, i have reflected boolean variables as follows:

activity = activityName(activities[i].substring(0,activities[i].lastIndexOf('.', activities[i].length() - 4)));  
Field field = refClass.getField(activity);  
Object obj = field;  
field.setBoolean(obj, true);

And that worked for me well. But now i need to use arrays instead of regular variables, and tried to make as follows:

activity = activityName(activities[i].substring(0, activities[i].lastIndexOf('.',  activities[i].length() - 4)));  
Field field = refClass.getField(activity);  
Object field_act = field;  
field_act.setBoolean(field_act, LMKStorage.currentLmkSlot, true);

And getting exception "Argument not an array". :(

In field_act.setBoolean(field_act, LMKStorage.currentLmkSlot, true);, field_act is boolean[] i am getting with .getField(activity), LMKStorage.currentLmkSlot is int to determine which position of an array to set and "true" is value to set. The field_act i have to get 100% is an array, because i have not non-array static variables in refClass.

so far i have got studing books i have.But still nothing. Tried to google any examples to update array elements... nothing usefull for me.

Please advice.

5
  • why are you using the field_act variable instead of field? Object doesn't have a setBoolean method. Can you post your actual code? Commented Nov 25, 2011 at 22:19
  • I'm a little confused by your working example. Your obj variable is the field representation object? obj should be some instance of your refClass. Commented Nov 25, 2011 at 22:22
  • Actually, i tried many ways, and field was too. activity = activityName(activities[i].substring(0, activities[i].lastIndexOf('.', activities[i].length() - 4))); Field field = refClass.getField(activity); Object field_act = field; Array.setBoolean(field, LMKStorage.currentLmkSlot, true); Just tried to use field` which is Field type and got: java.lang.IllegalArgumentException: Argument is not an array Commented Nov 25, 2011 at 22:37
  • Can't you just retrieve the array with Field.get, cast it to a boolean[], and set the value in that? Commented Nov 25, 2011 at 22:37
  • Sorry, for mistakes in formating posts, i am first time here and experimenting to get my posts nice. Commented Nov 25, 2011 at 22:38

1 Answer 1

1

For arrays, use java.lang.reflect.Array instead of java.lang.reflect.Field.

Object field_act = field.get(obj);
Array.setBoolean(field_act, LMKStorage.currentLmkSlot, true);
Sign up to request clarification or add additional context in comments.

2 Comments

Yess, You are correct that was my mistake while asking question. The correct code is: Array.setBoolean(field_act, LMKStorage.currentLmkSlot, true);
Then the mistake is in your code. You should replace Object field_act = field; with the line Object field_act = field.get(obj);.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.