Vaadin ValueChangeListener on select component doesn't work fine

Mario picture Mario · Oct 29, 2012 · Viewed 11.4k times · Source

I have a problem with ValueChangeListener. I have this code:

private void caricaSelectMainUser(){
    Object[] a = socFilName.get(0);
    List<String> elencosoc = societaDao.getSocieta();
    List<String> socUtente = app.getUserDao().getSocietaByUname(app.getCod_utente());   

    for (int i = 0; i < elencosoc.size(); i++) {            
        societa.addItem(elencosoc.get(i));
    }            
    caricaMenuFiliale();
    caricaMenuRisorsa();
    societa.select(socUtente.get(0));   
    filiale.select(a[1]);
    nome.select(a[2]);

    societa.addListener(new Property.ValueChangeListener() {

        @Override
        public void valueChange(ValueChangeEvent event) {
            filiale.removeAllItems();
            caricaMenuFiliale();
            nome.removeAllItems();
        }   
    });   

    filiale.addListener(new Property.ValueChangeListener() {

        @Override
        public void valueChange(ValueChangeEvent event) {
            System.out.println("entro nel listener filiale");
            nome.removeAllItems();
            caricaMenuRisorsa();
        }   
    });       

    nome.addListener(new ValueChangeListener() {

        @Override
        public void valueChange(ValueChangeEvent event) {
            getValuesFromDropDown();
        }   
    });       

    getValuesFromDropDown();}

This part of code manage the listeners for "società","filiale" and "name".

private void caricaMenuFiliale() {
    List<Object[]> elencofilNew = app.getUserDao().getDescFilialeSoc(societa.getValue().toString());
    mapFiliale = new HashMap<String,Object>();
    for (int i = 0; i < elencofilNew.size(); i++) {
        Object[] a = elencofilNew.get(i);
        for (int j = 0; j < a.length; j++) {
            mapFiliale.put(a[1].toString(), a[0]);
            filiale.addItem(a[1]);
        }
    }
}

private void caricaMenuRisorsa() {
    List<Object[]> elencoNominativiNew = app.getUserDao().getNominativiFromSoc(societa.getValue().toString());
    map = new HashMap<String,Object>();
    for (int i = 0; i < elencoNominativiNew.size(); i++) {
        Object[] a = elencoNominativiNew.get(i);
        map.put((String) a[0],a[1]);                        
        nome.addItem((String) a[0].toString());

    }
}

private void getValuesFromDropDown() {
    String codsoc = societa.getValue().toString();
    String codfil = mapFiliale.get(filiale.getValue().toString()).toString();
    String codper = map.get(nome.getValue()).toString();
    String mm = (mese.getValue().toString().length()==1?("0"+mese.getValue().toString())emoticonmese.getValue().toString()));
    String aaaamm = anno.getValue().toString() + mm;
}

the sequence flow should be that when "societa" change, select "filiale" is load with data bind with the selected "society" and when "filiale" change "name" is load with data bind with "society" and "filiale" selected. But when I select a new "society", the code below start

nome.addListener(new ValueChangeListener() {

    @Override
    public void valueChange(ValueChangeEvent event) {
        getValuesFromDropDown();
    }   
});    

and I catch the NullPointerException on

String codfil = mapFiliale.get(filiale.getValue().toString()).toString();

in getValuesFromDropDown() method. If I cancel this method call from valueChangeListener from select "nome" all works fine. I know that my logic could have some problems, but I don't know why this behavior. Could anyone help me?

Thank you.

Answer

vivid_voidgroup picture vivid_voidgroup · Nov 23, 2012

In the societa listener you call nome.removeAllItems(). That will trigger Property.ValueChangeEvent in the nome AbstractSelect.

societa.addListener(new Property.ValueChangeListener() {
    @Override
    public void valueChange(ValueChangeEvent event) {
        filiale.removeAllItems();
        caricaMenuFiliale();
        nome.removeAllItems();
    }   
});