使用可用课程填充组合框时,基数违规和NullPointerException

时间:2014-12-29 03:07:55

标签: java sql combobox nullpointerexception cardinality

我是java编程的相对论,无论如何我想做什么

我正在尝试使用学生可用的课程填充combobox1(添加列表),然后删除所选项目并发送到combobox2(下拉列表)

但它根本没有做出反应,特别是当学生没有任何可以添加的课程并且它返回异常时 " net.ucanaccess.jdbc.ucanaccessSQLException:基数违规" &安培; "显示java.lang.NullPointerException"

这里是如何初始化每个组合框

try 
    {String mail=Login.user_mail;            

        String sql = "select CourseCode from Course where CourseCode!=(select CourseCode from CourseStudent where StudentEmail='"+mail+"') OR NOT EXISTS(select CourseCode from CourseStudent where StudentEmail='"+mail+"')";
        java.sql.ResultSet rs = dbm.select(sql);
        DefaultComboBoxModel  m = new DefaultComboBoxModel();

        while (rs.next()) {
            m.addElement(rs.getObject(1).toString());
            }
            jComboBox1.setModel(m);

        }

        catch(Exception ex)
                {
                    JOptionPane.showMessageDialog(null, ex.toString());

                }    

然后是组合框2

     String mail=Login.user_mail;
        String sql = "select CourseCode from CourseStudent where StudentEmail='"+mail+"'";
        java.sql.ResultSet rs = dbm.select(sql);
        DefaultComboBoxModel  m = new DefaultComboBoxModel();

        while (rs.next()) {
            m.addElement(rs.getObject(1).toString());
            }
            jComboBox2.setModel(m);

        }    

这是在添加按钮

String ID = (String) jComboBox1.getSelectedItem();
    String mail=Login.user_mail;

            try {
                dbm.insert(" insert into CourseStudent (StudentEmail, CourseCode ) values ('"+mail+"', '"+ID+"') ");

                                    jComboBox2.addItem(jComboBox1.getSelectedItem());
                                    jComboBox1.removeItem(jComboBox1.getSelectedItem());
            }  catch (Exception ex)
    {
        JOptionPane.showMessageDialog(null, "Insertion Faild\n"+ex);   
    }    

这是使用下拉按钮

try {
                    String ID = (String) jComboBox2.getSelectedItem();
                    String mail=Login.user_mail;
                dbm.delete("delete from CourseStudent where CourseCode='"+ID+"' And StudentEmail='"+mail+"'");
                jComboBox1.addItem(jComboBox2.getSelectedItem());
                                    jComboBox2.removeItem(jComboBox2.getSelectedItem());
            } catch (Exception e) {

                JOptionPane.showMessageDialog(null, "Delete Faild\n"+e); 
            }    

我认为这个问题存在于combobox1 SQl语句中,但我真的看不到任何其他方法,因为我需要从表中检索课程,同时确保用户不会它已经从另一张桌子......

任何帮助都会非常感激,因为我说我已经使用java几个星期了,而且我之前没有使用过sql所以我有点迷失

0 个答案:

没有答案