程序中的java.lang.NullPointerException

时间:2014-03-31 08:38:31

标签: java

我的程序中一直出现以下错误。任何syntext或任何错误都没有错误

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

如果有人能帮我解决这个问题,我会很感激。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

如上所述,peter.petrov在声明boolean found之前添加验证。

  //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 = ...
   }