0

I'm creating a frame that contains 3 combobox each one is dependent to the other , the 3rd dependent to 2nd and the 2nd is dependent to 1st . The problem is when i change the first i get NullPointer exception on the 3rd because it's actions the change action for the 2nd .

My question is how to prevent the action of item change on the 3rd jComboBox "jCombobox2" when i change the 1st jComboBox "jComboBox0" ?

Here is my code :

    private void jComboBox0ItemItemStateChanged(ItemEvent event) {
    jComboBox1.removeAllItems();
    ComboItem cat = (ComboItem) jComboBox0.getSelectedItem();


    String requete = "from Subcategory where Fk_Category = " + cat.getValue();

    Collection subcategories = Subcategory.getListeSubcategory(requete);

    for (Iterator i = subcategories.iterator(); i.hasNext();) {
        Subcategory item = new Subcategory();
        item = (Subcategory) i.next();
        System.out.println(item.getId());

        jComboBox1.addItem(new ComboItem(item.getNom(), (int) item.getId()));
    }

    // System.out.println("tbdlat a lkhra ! : "+listCategory.get(0));

}

private void jComboBox1ItemItemStateChanged(ItemEvent event) {
    // nda2
    jComboBox2.removeAllItems();
    ComboItem cat = (ComboItem) jComboBox1.getSelectedItem();


    String requete = "from Area  where fk_Subcategory = " + cat.getValue()+" group by Nom_Area";

    Collection areas = Area.getListeArea(requete);

    for (Iterator i = areas.iterator(); i.hasNext();) {
        Area item = new Area();
        item = (Area) i.next();
        System.out.println(item.getId());

        jComboBox2.addItem(new ComboItem(item.getNom(), (int) item.getId()));
    }

}

private void jComboBox2ItemItemStateChanged(ItemEvent event) {
    // i'll do some code here
}
Lizinh
  • 71
  • 1
  • 1
  • 11
  • 5
    1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 27 '16 at 10:59
  • @AndrewThompson my question is how to prevent the action of item change on the 3rd jComboBox "jCombobox2" when i change the 1st jComboBox "jComboBox0" ? – Lizinh Jul 27 '16 at 13:37
  • 1
    OK.. good call for editing it into the question. Have you solved the problem with help from my first comment? If not, how is the MCVE as mentioned in my 2nd comment going? – Andrew Thompson Jul 27 '16 at 13:51

1 Answers1

0

The problem with your code seems to be that you do not check if an element is selected. You should check inside your listener if an item is selected, if you do not check that, the itemStateChange method will be called twice.

I post below a code which has three combo boxes and a single listener for all three combos :

    import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;

public class MainWindow1 extends JFrame implements ItemListener {

    public MainWindow1() {

        String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };

        //Create the combo box, select item at index 4.
        //Indices start at 0, so 4 specifies the pig.

        setLayout(new FlowLayout());

        JComboBox petList1 = new JComboBox(petStrings);
        //petList1.setSelectedIndex(4);
        petList1.addItemListener(this);
        petList1.setName("petList1");

        JComboBox petList2 = new JComboBox(petStrings);
        petList2.setSelectedIndex(4);
        petList2.addItemListener(this);
        petList2.setName("petList2");

        JComboBox petList3 = new JComboBox(petStrings);
        petList3.setSelectedIndex(4);
        petList3.addItemListener(this);
        petList3.setName("petList3");

        getContentPane().add(petList1);
        getContentPane().add(petList2);
        getContentPane().add(petList3);


        setSize(800, 600);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MainWindow1();

    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e != null && e.getSource().toString() != null && e.getSource().toString().contains("petList1") && e.getStateChange() == ItemEvent.SELECTED) {

            System.out.println( "1" + e.getSource());
        } else if (e != null && e.getSource().toString() != null && e.getSource().toString().contains("petList2") && e.getStateChange() == ItemEvent.SELECTED) {
            System.out.println( "2" + e.getSource());
        } else if (e != null && e.getSource().toString() != null && e.getSource().toString().contains("petList3") && e.getStateChange() == ItemEvent.SELECTED) {

            System.out.println("3" + e.getSource());
        }

    }

}
aurelianr
  • 538
  • 2
  • 12
  • 35