0

I am keep on getting following error in my program. There is nothing wrong with any syntext or any errors also

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)

This is my java code

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

I would be thankful if anyone could help me with this

3
  • 1
    The error is line 155, set a breakpoint there and check which object is null. Then determine why it is null. Commented Mar 31, 2014 at 8:41
  • @Sala line 155 is this (itemObjUserName.getUserName()).equalsIgnoreCase(txtUsername.getText()) && (itemObjUserName.getPassword()).equalsIgnoreCase(txtPassword.getText()); if (found == true) Commented Mar 31, 2014 at 8:54
  • then one of itemObjUserName, txtUsername and txtPassword has a null value, use a break point and debug which one is null. Commented Mar 31, 2014 at 8:57

2 Answers 2

1

You have a reference (say x) on line 155 which is null and you're calling x.someMethod or x.someField on it. Check line 155 and see what is null there.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your comment. this is what in line 154 and 155 boolean found (itemObjUserName.getUserName()).equalsIgnoreCase(txtUsername.getText()) && (itemObjUserName.getPassword()).equalsIgnoreCase(txtPassword.getText()); if (found == true) { so i have the problem with when user enters the wrong user name and password but according to the code i have written it is supposed to check the wrong user name and password for 3 times
Then... one of these is probably null, check them: itemObjUserName, txtUsername, txtPassword.
selected login details successfully m getting to the itemObjUser according to the entered txtUserName. so theres no null values in those itemObjUserName, txtUsername and txtPassword ..
0

As indicated above by peter.petrov add a validation before the declaration of 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 = ...
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.