我的组合框在第一次运行时填充并完美运行,但是一旦断开连接并再次连接,它就不会重新填充
我的方法填充组合框
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方法设置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");
}
}
我的连接和组合框按钮
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 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)