dimanche 22 mai 2016

Why am i getting a stackoverflow error when i try to get a object from a field using java reflection?

I'm trying to get fields from a JFrame class and separate those field objects by their type.Then im gonna put those separated objects into different Sets. But when i run the code, i'm getting a StackOverFlowError. I don't know what i'm doing wrong. Anyway this is my first time using java reflection, therefore i cannot be 100% sure that those particular code lines are suitable for get the necessary output. Please help me guys. Appreciate it. This is the code.

    public class SkinAnalyser {

private Set<JLabel> lblSet;
private Set<JTextField> txtSet;
private Set<JPanel> pnlSet;
private SkinColorInitializer clrInit;


    public SkinAnalyser(Field[] fields) throws IllegalArgumentException, IllegalAccessException{
        lblSet = new HashSet();
        txtSet = new HashSet();
        pnlSet = new HashSet();

        for (Field field : fields) {
            field.setAccessible(true);

            if(field.getType().isAssignableFrom(JLabel.class)){

                lblSet.add((JLabel)field.get(new EditUser()));
            }else if(field.getType().isAssignableFrom(JTextField.class)){

                txtSet.add((JTextField)field.get(new EditUser()));
            }else if(field.getType().isAssignableFrom(JPanel.class)){

                pnlSet.add((JPanel)field.get(new EditUser()));
            }
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire