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.
field_actvariable instead offield?Objectdoesn't have asetBooleanmethod. Can you post your actual code?fieldwas 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 usefield` which isFieldtype and got: java.lang.IllegalArgumentException: Argument is not an arrayField.get, cast it to aboolean[], and set the value in that?