Hello,

I have 3 combobox and I want to find them in my table "class" and retrieve the ID.
I am currently having trouble with the following code:

        public void retrieveClass(String time,String stage, String teacher){
       Connection con = null;
    ResultSet rs = null;
   java.sql.PreparedStatement st = null;
   Properties conProps = new Properties();
        conProps.setProperty("user", "root");
        conProps.setProperty("password", "root");

       try {
            con =DriverManager.getConnection ("jdbc:mysql://localhost:3306/database", conProps);
           String sql = ("select*from class where Stage = ? and Teacher = ? and Time =? ");  

 st.setString(1,time);
st.setString(2,stage);
  st.setString(3,teacher);

            st.executeQuery();
            rs = st.getResultSet();

while (rs.next()) {
    String teach = rs.getString("ClassID");

txtid.setText(teach);
}

    con.commit();
}  catch (Exception e) {


}


    }

For the button I have this code:

   String a = cb5.getSelectedItem().toString();
       String b = cb6.getSelectedItem().toString();
       String c = cb7.getSelectedItem().toString();

        retrieveClass(a,b,c)

I want to retrieve the classID in the textfield. It doesn't do anything.

  1. Never, that's NEVER, have an empty catch block when you are devoping code (lines 27-30). How will you know if Java has detected an error? Always start with an e.printStackTrace(); in every catch block.
  2. If you do that, but there are no exceptions thrown, then add print statements to confirm key info like: the values of the parameters passed to the method and the size of the result set. But first, e.printStackTrace();

Edited 4 Years Ago by JamesCherrill

I did that before hand, there is nothing happening. And no exceptions.

You really put in an e.printStackTrace(); then took it out again? That's novel.
And you put in the print statements but nothing was printed out? In that case your method wasn't being called so the problem lies elsewhere.

Sorry my bad! I now have a null Pointer exception.

Edited 4 Years Ago by london-G

If you put in the print statements I suggested then you would know what the values were that were taken from the GUI and passed to the method, and you would know that your select was returning zero rows, and you would know why.

I have a null pointer exception at line 13. I also changed this line here:

       String sql = ("select*from class where Time =? and Stage = ? and Teacher = ?  "); 

String sql = ("selectfrom class where Time =? and Stage = ? and Teacher = ? ");
String sql = ("select
from class where Stage = ? and Teacher = ? and Time =? ");
All you changed was the order - which has no significance in SQL - so why bother?

The NPE is hardly surprising...
Line 4: java.sql.PreparedStatement st = null;
Lines 5-12: no assignments to st
Line 13: st.setString(1,time);
... but what is surprising is that earlier you said you had no exceptions.

You seem to be thrashing around here, posting contradictory (or at least confusing) info, and not doing any basic debugging with print statements. So that people can help, I suggest you 1. Fix the NPE then 2. add some debug prints the 3. post the latest version of the code along with the exact output it produces (from print statements or printStackTrace). Of course, you are free to accept as little or as much of this advice as you want.

Thank you! I am sorry for the confusion lol. I am not great as you are and I am still learning. Anyway my problem is now fixed thanks to you.

This question has already been answered. Start a new discussion instead.