Hi.
I have two tables in mysql - customer_master and contact_master.
customer_master has a primary key - "cust_id" ,which is a foreign key in contact_master and "cust_name" which is also a foreign key in contact_master.
I have a frame which will do CRUD operations for contact_master.
The frame has two two comboboxes, one for "cust_id" and other for "cust_name".
When the form loads, the comboboxes are filled with the resultset which contains records <cust_id, cust_name > retrieved from cutomer_master.
Now, when I select a particular cust_id from the first combobox, the second combobox should reflect the corresponding cust_name and vice-versa
I tried using both ActionPerformed and ItemChanged events.
Here's my code:

private void reset_cont_master_Fields(){
    Cust_ID_Cont_Mstr_ComboBox.removeAllItems();//remove all previous items 
    Company_Cont_Mstr_ComboBox.removeAllItems();//from the lists
    
    try {//since these are combo boxes, query has to execeuted and resultset filled into them
        
        R2 = S.executeQuery("select cust_id, cust_name from Customer_master order by cust_id;");
        //R.first();
        while (R2.next()) {
            Cust_ID_Cont_Mstr_ComboBox.addItem(R2.getString("cust_id"));
            Company_Cont_Mstr_ComboBox.addItem(R2.getString("cust_name"));
        }
    } catch (SQLException e) {SQL_fetch_error_Dialog.where_error_has_occured = e.getMessage();//flash error..
                              SQL_fetch_error_Dialog.main(args);System.out.println(e);
       }//catch ends

This is the ItemStateChanged event:

private void Cust_ID_Cont_Mstr_ComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {                                                            
     //System.out.println(Cust_ID_Cont_Mstr_ComboBox.getSelectedItem().toString());
     int indx = Cust_ID_Cont_Mstr_ComboBox.getSelectedIndex();
     Company_Cont_Mstr_ComboBox.setSelectedIndex(indx);
}

When reset_cont_master_Fields() is called, I get

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: setSelectedIndex: 0 out of bounds
        at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:601)
        at masterentry.MasterEntry_Frame.Cust_ID_Cont_Mstr_ComboBoxItemStateChanged(MasterEntry_Frame.java:1031)
        at masterentry.MasterEntry_Frame.access$900(MasterEntry_Frame.java:23)
        at masterentry.MasterEntry_Frame$12.itemStateChanged(MasterEntry_Frame.java:565)
        at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1205)
        at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1262)
        at javax.swing.JComboBox.contentsChanged(JComboBox.java:1309)
        at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:100)
        at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:88)
        at javax.swing.DefaultComboBoxModel.addElement(DefaultComboBoxModel.java:126)
        at javax.swing.JComboBox.addItem(JComboBox.java:696)
        at masterentry.MasterEntry_Frame.reset_cont_master_Fields(MasterEntry_Frame.java:927)

What shud I do?
thanks..

The exception is thrown when the index is >= size, so it looks like there is nothing in Company_Cont_Mstr_ComboBox when this code is executed. (Maybe? happens when first cb is populated = fires change event, before 2nd is populated? Test size of Company_Cont_Mstr_ComboBox before setting its index in the change listener?)

OK...I solved the problem, but partially..
When the frame is loaded for the first time, it populates the comboboxes properly...
But when I click on 'New' button, which shoud remove all the previous items and reexecute the query to refetchthe records, I get some exceptions at the point where it tries to remove items from "Cust_id" combobox..

Here's my code:

private void reset_cont_master_Fields(){Cust_ID_Cont_Mstr_ComboBox.removeAllItems();//
    Cust_ID_Cont_Mstr_ComboBox.removeAllItems();//remove all previous items
    Company_Cont_Mstr_ComboBox.removeAllItems();//from the lists
    
    try {//since these are combo boxes, query has to execeuted and resultset filled into them
        flag_betn_Comboxs = 0;
        R2 = S.executeQuery("select cust_id, cust_name from Customer_master order by cust_id;");
        //R.first();
        while (R2.next()) {
            Cust_ID_Cont_Mstr_ComboBox.addItem(R2.getString("cust_id"));
            Company_Cont_Mstr_ComboBox.addItem(R2.getString("cust_name"));
        }
        flag_betn_Comboxs = 1;//This indicates all the items have been filled and thus dues not cause ItemState-
                              //Changed event to be triggered everytime an item is added.
   
    } catch (SQLException e) {SQL_fetch_error_Dialog.where_error_has_occured = e.getMessage();//flash error..
                              SQL_fetch_error_Dialog.main(args);System.out.println(e);
                             }//catch ends

Here's the NewButtonClick event:

private void New_Cont_Mstr_ButtonMouseClicked(java.awt.event.MouseEvent evt) {                                                  
    SaveFlag2 = 0;//insert query can be executed..
    reset_cont_master_Fields();//Blank the fields
}

Here are the actionlisteners

private void Cust_ID_Cont_Mstr_ComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                                           
   if(flag_betn_Comboxs == 0)return;//if all items are not filled, dont let control go down..

   //int indx = Cust_ID_Cont_Mstr_ComboBox.getSelectedIndex();
//   Company_Cont_Mstr_ComboBox.setSelectedIndex(indx);
    try {
    R2.beforeFirst();//beforefirst();

    while (R2.next()) {
            if (Cust_ID_Cont_Mstr_ComboBox.getSelectedItem().equals(R2.getString(1))) {
                    Company_Cont_Mstr_ComboBox.setSelectedItem(R2.getString(2));
                    break;
            }//if ends
       }//while ends
    } catch (SQLException sQLException) {}

        //flag_betn_Comboxs = 0;//reset flag
}                                                          

private void Company_Cont_Mstr_ComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                                           
       if(flag_betn_Comboxs == 0)return;//if all items are not filled, dont let control go down..
       try {
            R2.beforeFirst();//beforefirst();

            while (R2.next()) {
                if (Company_Cont_Mstr_ComboBox.getSelectedItem().equals(R2.getString(2))) {
                    Cust_ID_Cont_Mstr_ComboBox.setSelectedItem(R2.getString(1));
                    break;
                }
            }//while ends
          } catch (SQLException sQLException) {}

       //flag_betn_Comboxs = 0;//reset flag
}

Here's the exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at masterentry.MasterEntry_Frame.Cust_ID_Cont_Mstr_ComboBoxActionPerformed(MasterEntry_Frame.java:1002)
        at masterentry.MasterEntry_Frame.access$800(MasterEntry_Frame.java:24)
        at masterentry.MasterEntry_Frame$11.actionPerformed(MasterEntry_Frame.java:567)
        at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1240)
        at javax.swing.JComboBox.contentsChanged(JComboBox.java:1311)
        at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1331)
        at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:161)
        at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:169)
        at javax.swing.JComboBox.removeAllItems(JComboBox.java:751)
        at masterentry.MasterEntry_Frame.reset_cont_master_Fields(MasterEntry_Frame.java:917)
        at masterentry.MasterEntry_Frame.New_Cont_Mstr_ButtonMouseClicked(MasterEntry_Frame.java:954)
        at masterentry.MasterEntry_Frame.access$400(MasterEntry_Frame.java:24)
        at masterentry.MasterEntry_Frame$7.mouseClicked(MasterEntry_Frame.java:536)
        at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
        at java.awt.Component.processMouseEvent(Component.java:6219)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5981)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4583)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4229)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2475)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

When i click on New for the second time, it generates this:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at masterentry.MasterEntry_Frame.Company_Cont_Mstr_ComboBoxActionPerformed(MasterEntry_Frame.java:1018)
        at masterentry.MasterEntry_Frame.access$900(MasterEntry_Frame.java:24)
        at masterentry.MasterEntry_Frame$12.actionPerformed(MasterEntry_Frame.java:573)
        at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1240)
        at javax.swing.JComboBox.contentsChanged(JComboBox.java:1311)
        at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1331)
        at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:161)
        at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:169)
        at javax.swing.JComboBox.removeAllItems(JComboBox.java:751)
        at masterentry.MasterEntry_Frame.reset_cont_master_Fields(MasterEntry_Frame.java:919)
        at masterentry.MasterEntry_Frame.New_Cont_Mstr_ButtonMouseClicked(MasterEntry_Frame.java:954)
        at masterentry.MasterEntry_Frame.access$400(MasterEntry_Frame.java:24)
        at masterentry.MasterEntry_Frame$7.mouseClicked(MasterEntry_Frame.java:536)
        at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
        at java.awt.Component.processMouseEvent(Component.java:6219)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5981)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4583)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4229)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2475)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

When I click New for the third time, IT WORKS AS IT IS SUPPOSED TO!
Thanks...

It's hard to give help on these exceptions because (a) the listing is truncated and I can't see the line number for the critical error, and (b) without the program line numbers on the program listing I can't match up the exception to the statement that caused it. Null pointer exception means something is un-initialised or possible something is empty.
However, please try to interpret the exceptions yourself. The first line of the stack dump shows you the statement where it failed, and the text of the exception (first line) tells you what went wrong. 90% of the time that's all you need, and a further 9% of the time it tells you where to put a couple of debug prints to pinpoint th trouble. For the other 1%, there are many Java experts on this site!

Also, the "Working as it is supposed to" is just a figment of your imagination. If it doesn't work the first time, it is just tricking you the third time. Hehe.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.