JComboBox ExecuteQuery 选择项目错误

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


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


第一个 jComboBox actionperformed*


    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);

            }

        }

    }

但是在我选择另一个房间类型后它会起作用我试图删除“combobox.removeAllItems();” 但它会在近 1 周的时间内不断添加 jCombobox 中的所有项目,试图弄清楚有人可以帮忙吗


至尊宝的传说
浏览 103回答 2
2回答

跃然一笑

当你调用removeAllItems它时会触发 actionListenerjComoboBox14 并且在这个阶段它不会有任何 Items 所以getSelected会返回NULL改变你if的if(jComboBox14.getItemCount() > 0 && (jComboBox14.getSelectedItem().toString().equals("SELECT") |                                              jComboBox14.getSelectedItem().toString().isEmpty())){

HUWWW

首先。你应该给你的对象变量一个有用的名字:例如:jComboBox13 --> JComboBox comboRoomsType&nbsp; = new JComboBox();或者你喜欢的任何名字。然后,很高兴看到所有涉及的代码。我看不到你在哪里初始化ResultSetor PreparedStatement;我猜 NullPointer 在尝试检索值时来自 select 语句。if(jComboBox13.getSelectedItem().toString().equals("SELECT")){}else{&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; String like = jComboBox13.getSelectedItem().toString();&nbsp; &nbsp; &nbsp; &nbsp; String sql = "Select * From Room_Master RM " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"inner join Room_Type RT on RM.Room_Type_ID=RT.Room_Type_ID "&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+"where Room_Type = '"+like+"'";&nbsp; &nbsp; &nbsp; &nbsp; pst = conn.prepareStatement(sql);&nbsp; &nbsp; &nbsp; &nbsp; rs = pst.executeQuery();&nbsp; &nbsp; &nbsp; &nbsp; jComboBox14.removeAllItems();&nbsp; &nbsp; &nbsp; &nbsp; jComboBox14.addItem("SELECT");&nbsp; &nbsp; &nbsp;//you can also check if there are values first.&nbsp; &nbsp; while(rs.hasNext()){&nbsp; &nbsp; &nbsp; &nbsp; rs.next();&nbsp; &nbsp; &nbsp; &nbsp; String add1 = rs.getString("Room_No");&nbsp; &nbsp; &nbsp; &nbsp;//You can also use&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;//String add1 = rs.getInt(number of column of <Room_No> );&nbsp; &nbsp; &nbsp; &nbsp; jComboBox14.addItem(add1);&nbsp; &nbsp; }&nbsp; &nbsp; }catch(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, e);&nbsp; &nbsp; }finally {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rs.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pst.close();&nbsp; &nbsp; &nbsp; &nbsp; }catch(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java