繁体   English   中英

程序中的java.lang.NullPointerException

[英]java.lang.NullPointerException in the program

我一直在程序中出现以下错误。 任何文本或任何错误都没有错

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at PresentationLayer.AdminLogin.btnLoginActionPerformed(AdminLogin.java:155)
    at PresentationLayer.AdminLogin.access$000(AdminLogin.java:11)
    at PresentationLayer.AdminLogin$1.actionPerformed(AdminLogin.java:51)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)

这是我的Java代码

private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         

        String username = txtUsername.getText();
        Admin itemObjUserName = new Admin().getLoginDetailsDB(username);

        boolean found = (itemObjUserName.getUserName()).equalsIgnoreCase(txtUsername.getText()) && (itemObjUserName.getPassword()).equalsIgnoreCase(txtPassword.getText());
        if (found == true) {
            String message = "Welcome to the City Library Management System";
            JOptionPane.showMessageDialog(this, message);
            AdminMenu Obj = new AdminMenu();
            Obj.setVisible(true);
            this.dispose();

        } else {          
            if (count < 2) {
               count = count +1 ;
                if (count == 1) {
                    String message1 = "Invalid  Password.!.Warning 2 more Attempts left";
                    JOptionPane.showMessageDialog(this, message1);
                } else if (count == 2) {
                    String message2 = "Invalid  Password.!.Warning 1 more Attempt left";
                    JOptionPane.showMessageDialog(this, message2);
                } 
            } else {

                    String message3 = "Invalid Password.! & You are Temporarily Blocked for Exceeding Max Number of Login Attempts.Error";
                    JOptionPane.showMessageDialog(this, message3);
                    txtUsername.setVisible(false);
                    txtPassword.setVisible(false);  
                    btnLogin.setVisible(false);
            }
        }
    }                                        

如果有人可以帮助我,我将非常感激

您在第155行有一个引用(例如x ),该引用为null,并在其上调用x.someMethodx.someField 检查第155行,看看那里是否为null

如上面的peter.petrov所示,在找到的boolean声明之前添加一个验证。

  //declare variable
    boolean found = false;
  //verify that all elements are not null (and maybe not empty - it applies to getText() property)
    if(itemObjUserName != null && itemObjUserName.getUserName() != null && txtUsername != null 
&& txtUsername.getText() != null && itemObjUserName.getPassword() != null 
&& txtPassword != null && txtPassword.getText() != null)
   {
     found = ...
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM