0

My combo box populates and works perfectly the first run but as soon as i disconnect and connect again it doesn't repopulate

My method populating the combo box

    void ConnectDB(){
    ArrayList<String> idList =  new ArrayList<String>();

    try{
        if(btnConnectOption.getText()=="Connect"){
         Class.forName("com.mysql.jdbc.Driver");
        String database
                = "jdbc:mysql://localhost/CTIDB";
        Connection conn = DriverManager.getConnection(database, "root", "");
        java.sql.Statement run = conn.createStatement();
        btnConnectOption.setText("Disconnect");

        String selTable = "SELECT userID FROM studentInfo";



        run.execute(selTable);
        ResultSet rs = run.getResultSet();

        while (rs.next()) {
            String result = rs.getString(1);
            idList.add(result);
        }
        conn.close();
        for(int i=0;i<idList.size();i++){
        cboIDNums.addItem(idList.get(i));

          }
        connected = true;

        }
        else if (btnConnectOption.getText()=="Disconnect"){
            jtfFirstName.setText("");
            jtfAge.setText("");
            jtfLastName.setText("");

            btnConnectOption.setText("Connect");

            cboIDNums.removeAllItems();

            connected = false;


        }




    }catch(Exception e){
        System.out.println("wrong");
    }

My Search DB method to set the texfields

public void searchDB(int uID){
    try{ArrayList<String> userInfo =  new ArrayList<String>();

        ResultSet rs = null;
    Class.forName("com.mysql.jdbc.Driver");
        String database
                = "jdbc:mysql://localhost/CTIDB";
        Connection conn = DriverManager.getConnection(database, "root", "");

        PreparedStatement pstmt = null;

        String query = "Select firstName,lastName,Age from studentInfo WHERE userID ='"+uID+"'";

        pstmt = conn.prepareStatement(query);

        rs = pstmt.executeQuery();

        while (rs.next()) {
            userInfo.add(rs.getString(1));
            userInfo.add(rs.getString(2));
            userInfo.add(rs.getString(3));

        }
        conn.close();
        jtfLastName.setText(userInfo.get(0));
        jtfFirstName.setText(userInfo.get(1));
        jtfAge.setText(userInfo.get(2));


    }catch(Exception e){
        System.out.println("Wrong");
    }

}

My connect and combo box buttons

     private void btnConnectOptionActionPerformed(java.awt.event.ActionEvent evt) {                                                 
   ConnectDB();
}                                                

private void cboIDNumsActionPerformed(java.awt.event.ActionEvent evt) {                                          
    int x = Integer.parseInt(cboIDNums.getSelectedItem().toString());
    searchDB(x);
}                               

Stacktrace from Exception e:

    java.lang.NullPointerException
at Client.cboIDNumsActionPerformed(Client.java:274)
at Client.access$100(Client.java:23)
at Client$2.actionPerformed(Client.java:189)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1260)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1331)
at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1351)
at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179)
at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:174)
at javax.swing.JComboBox.removeAllItems(JComboBox.java:773)
at Client.ConnectDB(Client.java:90)
at Client.btnConnectOptionActionPerformed(Client.java:269)
at Client.access$200(Client.java:23)
at Client$3.actionPerformed(Client.java:196)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Wade Ferreira
  • 77
  • 1
  • 9
  • Without reading your code too thoroughly, I notice a couple of errors right off the bat- you are improperly comparing your strings. For example, the line `btnConnectOption.getText()=="Connect"` should be `btnConnectOption.getText().equals("Connect")`. Similarly for other string comparisons. Have a look at [this question](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – augray Aug 23 '15 at 21:26
  • Sorry, i am still new to programming. I was just focusing on getting rid of this bug and will fix the smaller problems once its done. – Wade Ferreira Aug 23 '15 at 21:31
  • I point it out because it may be the cause of your larger problem. When you compare the strings as you have, your tests for string equality will return false. Put a print statement or drop a breakpoint within the blocks that begin with `if(btnConnectOption.getText()=="Connect"){` and ` else if (btnConnectOption.getText()=="Disconnect"){`, and you'll see they never get executed. That's sure to cause big problems. – augray Aug 23 '15 at 21:33
  • I tried using a global boolean variable instead of the getText and it still didn't work. I changed the strings to .equals() now anyway though – Wade Ferreira Aug 23 '15 at 21:36
  • Have you used a debugger/print statements to see whether the database is returning empty results after the first try, or whether you're just not reaching the code that executes the query? – augray Aug 23 '15 at 21:51
  • The combobox isnt returning the selected item value to the method after I try to connect for the second time. The first time it works perfectly but the second time it's as if it stops listening. – Wade Ferreira Aug 23 '15 at 21:56
  • Right, but there are multiple possibilities for this behavior. Two obvious ones are 1) Your query isn't returning a result. 2) You aren't getting to the query. In `searchDB`, put a print statement in your loop. Does it print? – augray Aug 23 '15 at 22:04
  • I tried and it doesn't print. If i remove the `cboIDNums.removeAllItems();` from `connectDB` then it works but it keeps adding more items to the combobox. – Wade Ferreira Aug 23 '15 at 22:07
  • That means that your query isn't returning any results the second time. This can [apparently happen if you have multiple simultaneous connections](http://stackoverflow.com/questions/4343078/jdbc-returning-empty-result-set). This could be because you're not actually closing your exception the first time. Do you see "Wrong" printed when you click your connect button? – augray Aug 23 '15 at 22:17
  • Yes I do, but I closed the connection just under the result set? – Wade Ferreira Aug 23 '15 at 22:20
  • Yeah, you should almost always use connections with a close in a [finally block](https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html), or declare the connection in a [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html). Also, can you please add e.printStackTrace() after printing Wrong, then add the stack trace to your question? – augray Aug 23 '15 at 22:25
  • Okay I have added the close inside a finally block, and added the stack trace to my question. – Wade Ferreira Aug 23 '15 at 22:30
  • What's on line 274 of your source file? – augray Aug 23 '15 at 22:34
  • Its the `cboIDNumsActionPerformed` button, `int x = Integer.parseInt(cboIDNums.getSelectedItem().toString());` is line 274 – Wade Ferreira Aug 23 '15 at 22:35
  • Specifically, what is the line of code at that location? – augray Aug 23 '15 at 22:43
  • I just wrapped that line in an if checking if its not null and only then sends it. Thanks so much for all the help, I wouldnt have realised without it. – Wade Ferreira Aug 23 '15 at 22:45

0 Answers0