JComboBox ExecuteQuery选择项目错误

时间:2019-02-04 06:54:08

标签: java sqlite

我有3个jComboBox。第一个是房间类型。当我在第一个jComboBox上选择“房间类型”时,它必须在第二个jComboBox中显示所有可用的房间,但是当我选择一个“房间类型”时,会弹出错误消息。

https://imgur.com/a/wpmhLfD

这是在第一个jComboBox上执行的操作的代码

第一个jComboBox操作执行*

    if(jComboBox13.getSelectedItem().toString().equals("SELECT")){

    }else{
            try{
            String like = jComboBox13.getSelectedItem().toString();
            String sql = "Select * From Room_Master\n" +
                         "inner join Room_Type on Room_Master.Room_Type_ID=Room_Type.Room_Type_ID\n" +
                         "where Room_Type = '"+like+"'";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            jComboBox14.removeAllItems();
            jComboBox14.addItem("SELECT");
        while(rs.next()){
            String add1 = rs.getString("Room_No.");
            jComboBox14.addItem(add1);
        }
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }finally {
            try {
               rs.close();
               pst.close();
            }catch(Exception e){

            }
        }
    }

执行了第二次jComboBox操作

if(jComboBox14.getSelectedItem().toString().equals("SELECT") | jComboBox14.getSelectedItem().toString().isEmpty()){

    }else{
            try{

            String like = jComboBox14.getSelectedItem().toString();
            String sql = "Select * from Bed_Master\n" +
                         "inner join Room_Master on Bed_Master.Room_ID=Room_Master.Room_ID\n" +
                         "where [Room_No.] = '"+like+"'";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            jComboBox15.removeAllItems();
            jComboBox15.addItem("SELECT");
        while(rs.next()){
            String add1 = rs.getString("Bed_No.");
            jComboBox15.addItem(add1);
        }
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            e.printStackTrace();
        }finally {
            try {
               rs.close();
               pst.close();
            }catch(Exception e){

            }
        }
    }

但是在我选择其他房间类型后,它将起作用 我试图删除“ combobox.removeAllItems();” 但它将继续添加jCombobox中的所有项目 试图弄清楚将近1个星期,有人可以帮忙吗

2 个答案:

答案 0 :(得分:1)

调用removeAllItems时,它会触发jComoboBox14的actionListener 并且在此阶段它将没有任何项目,因此getSelected将返回NULL

将您的if更改为

if(jComboBox14.getItemCount() > 0 && (jComboBox14.getSelectedItem().toString().equals("SELECT") |
                                             jComboBox14.getSelectedItem().toString().isEmpty())){

答案 1 :(得分:0)

首先。您应该给对象变量一个有用的名称: 例如:jComboBox13 --> JComboBox comboRoomsType = new JComboBox();或您喜欢的任何名称。

然后,很高兴看到所有涉及的代码。我看不到在何处初始化ResultSetPreparedStatement;

我想当尝试检索值时,NullPointer来自select语句。

if(jComboBox13.getSelectedItem().toString().equals("SELECT")){

}else{
        try{
        String like = jComboBox13.getSelectedItem().toString();
        String sql = "Select * From Room_Master RM " +
                     "inner join Room_Type RT on RM.Room_Type_ID=RT.Room_Type_ID " 
                     +"where Room_Type = '"+like+"'";
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        jComboBox14.removeAllItems();
        jComboBox14.addItem("SELECT");
     //you can also check if there are values first.
    while(rs.hasNext()){
        rs.next();
        String add1 = rs.getString("Room_No");
       //You can also use 
       //String add1 = rs.getInt(number of column of <Room_No> );
        jComboBox14.addItem(add1);
    }
    }catch(Exception e){
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e);
    }finally {
        try {
           rs.close();
           pst.close();
        }catch(Exception e){

        }
    }
}