2014-03-31 33 views
0

我一直在我的程序中出現錯誤。沒有什麼錯與任何SYNTEXT或任何錯誤也程序中的java.lang.NullPointerException

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

哪條線是155? – Salah

+1

錯誤是第155行,在那裏設置斷點並檢查哪個對象爲空。然後確定它爲什麼是空的。 – Fredrik

+0

@Sala 線155是本 (itemObjUserName.getUserName())equalsIgnoreCase(txtUsername.getText())&&(itemObjUserName.getPassword())equalsIgnoreCase(txtPassword.getText())。。如果(found == true) – user3003900

回答

1

你有一個參考(說x)在線155是空的,你打電話x.someMethodx.someField就可以了。檢查第155行,看看那裏有什麼是null

+0

感謝您的評論。這是行154和155中的內容 boolean found(itemObjUserName.getUserName())。equalsIgnoreCase(txtUsername.getText())&&(itemObjUserName.getPassword())。equalsIgnoreCase(txtPassword.getText()); if(found == true){ 所以我有問題,當用戶輸入錯誤的用戶名和密碼 ,但根據我寫的代碼應該檢查錯誤的用戶名和密碼3次 – user3003900

+0

然後......其中一個可能是'null',請檢查它們:'itemObjUserName,txtUsername,txtPassword'。 –

+0

根據輸入的txtUserName,成功選擇登錄詳細信息到達itemObjUser。所以在這些itemObjUserName,txtUsername和txtPassword中沒有空值。 – user3003900

0

如上面peter.petrov所示,在發現布爾值聲明之前添加驗證。

//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 = ... 
    } 
相關問題