Is there a problem with my code? Finding null pointer Exception

29 Views Asked by At

I have developed an app login page. The app runs fine, but then crashes halfway through installation. I am new to app development, and aware this is a novice problem. I have looked at the logcat, it does state Java. lang. null pointer Exception, which from my understanding means I have clarified an object which I have called upon in my code. Would you mind pointing out, what exactly I have missed. With thanks.

Again it is not fully running, and the app will not install on my phone. Looking at the logcat it also makes a statement. Void android. widget. Button. set on.clicklistener(androidview.onclick listener). I have tried to add to the code setcontent.view equation, but that only seemed to cause errors.

Here is my code.

Public class MainActivity extends AppCompatActivity {

    private EditText eName;
    private EditText ePassword;
    private Button eLogin;
    private TextView eAttemptsInfo;
    private final String Username = "Admin";
    private final String Password = "12345678";
    Boolean isValid = false;
    private int counter=5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(counter);
        eName = findViewById(R.id.etname);
        ePassword = findViewById(R.id.etPassword);
        eLogin = findViewById(R.id.btnLogin);
        eAttemptsInfo = findViewById(R.id.tvAtemptsinfo);

        eLogin.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                String inputName = eName.getText().toString();
                String inputPassword = ePassword.getText().toString();

                if (inputName.isEmpty() || inputPassword.isEmpty())


                {
                    Toast.makeText(MainActivity.this, "Please enter all details correctly.", Toast.LENGTH_SHORT).show();
                }else {

                    isValid = validate(inputName, inputPassword);
                    if (!isValid)

                        counter--;
                    Toast.makeText(MainActivity.this, "Incorrect details entered.", Toast.LENGTH_SHORT).show();
                    eAttemptsInfo.setText("No. of attempts remaining = "+counter);

                    if (counter == 1) {

                        eLogin.setEnabled(false);
                    {
                    }


                        {

                        }



                    } else

                        Toast.makeText(MainActivity.this, "Login was sucessfull.", Toast.LENGTH_SHORT).show();
                    // Add the code to go to new activity
                    Intent intent=new Intent(  MainActivity.this,HomePageActivity.class);
                    startActivity(intent);
                    {

                    }


                }


            }

            private boolean validate(String name, String password) {
                return name.equals(Username) && password.equals(Password);
            }
        });
    }
0

There are 0 best solutions below