对数据库查询的Nullpointexeption

时间:2018-11-15 23:38:42

标签: java nullpointerexception access

我有一个带有以下代码的按钮。

 private void jLabel72MouseClicked(java.awt.event.MouseEvent evt) {                                      
    newtl = managertran.getSelectedItem().toString();
    vaup = va.toUpperCase();
    try {
        con2 = Connectionz.getConnection();//Connection Object
        pst4 = con2.prepareStatement("SELECT [VA #] FROM [Login_Users] WHERE [Full Name] =?");
        pst4.setString(1, newtl);
        rs4 = pst4.executeQuery();
        if (rs4.next()) {
            newtlva = rs4.getString("VA #");
            con2.close();

        } } 
     catch (Exception ex) {
                System.out.println("" + ex);
            }
    agentext = agentex1.getText();
    try {
        con2 = Connectionz2.getConnection();//Connection Object
        pst4 = con2.prepareStatement("SELECT * FROM [First_Entry] WHERE [VA_Agent] =?");
        pst4.setString(1, agentext);
        rs4 = pst4.executeQuery();
        if (rs4.next()) {
            exagentname = rs4.getString("Name_Agent");
            supername = rs4.getString("TL_Agent");
            con2.close();

        }
         } catch (Exception ex) {
        System.out.println("" + ex);
        JOptionPane.showMessageDialog(null, "No change was done, no entries were found.1");
    }       
        if (supername.equalsIgnoreCase(vaup)) {
            try {
                int yesorno2 = JOptionPane.showConfirmDialog(null, "Are you sure that you would like to change the user " + exagentname + " to the team lead " + newtl + "?", "exit", JOptionPane.YES_NO_OPTION);
                if (yesorno2 == 0) {
                    con2 = Connectionz2.getConnection();
                    String exchangeag = "UPDATE [First_Entry] SET [TL_Agent] =? WHERE [VA_Agent] =? AND [TL_Agent] =?";
                    con2 = Connectionz2.getConnection();
                    pst2 = con2.prepareStatement(exchangeag);
                    pst2.setString(1, newtlva);
                    pst2.setString(2, agentext);
                    pst2.setString(3, vaup);
                    pst2.execute();
                    JOptionPane.showMessageDialog(null, "Done!");
                                         agentex1.setText(" ");

                }
            } catch (HeadlessException | SQLException | NullPointerException ex) {
                System.out.println("" + ex);
                JOptionPane.showMessageDialog(null, "No change was done, no entries were found.");

                         }
        } else {
            JOptionPane.showMessageDialog(null, "You Don't have the enough privileges to change this agent");

        }

要继续将代码转到数据库,请收集先前提供的文本框和组合框的几个信息,并更改和更新具有该条件的另一个条目,这种情况是,并非总是应该更新的数据库才有一个用户创建后,就无法找到应该更新的寄存器,因此我在线程“ AWT-

中接收到错误Exception
EventQueue-0" java.lang.NullPointerException
    at Signin_Panel.jLabel72MouseClicked(Signin_Panel.java:3994)

那是行

   if (supername.equalsIgnoreCase(vaup)) 

我希望能够发送一条消息,通知没有用户该系统无法完成该操作,但是我无法捕获触发异常的位置,因此无法显示该消息,我我是一名新手开发人员,所以请不要与我同在,欢迎任何好的反馈!谢谢!

1 个答案:

答案 0 :(得分:0)

我认为查询没有问题。 null指针异常位于此函数 supername.equalsIgnoreCase(vaup)上,因此您必须检查此函数的值是什么,尤其是 vaup的值 vaup = va.toUpperCase(); va 不为空吗?)

致谢