mardi 14 mai 2019

JavaBean setter method invocation error "wrong number of arguments"

Im trying to set indexed value of JavaBean and i cannot do that with reflection. Any ideas why is this happening? How to invoke setter by reflection?

public class Bean1111 {
    public void setColors(Color[] colors) {
        this.colors = colors;
    }
    public Color [] colors = {Color.RED, Color.green, Color.blue, Color.pink};

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        Bean1111 bean = new Bean1111();
        Color[] colors = new Color[]{Color.RED,Color.BLACK};
        bean.getClass().getDeclaredMethods()[0].invoke(bean, colors); //exception  "java.lang.IllegalArgumentException: wrong number of arguments"
    }
}





Aucun commentaire:

Enregistrer un commentaire