0

Hello I am new to android java codding I created a "login activity" and "Users class" and my condition is to add a number of users and pass to the "Users class" and check if the password given is correct then log in, however, it works correctly when I put the right password but if the password is wrong the app just crashes and the if-else condition does not run.

public class MainActivity extends AppCompatActivity {
    private EditText Name;
    private EditText Password;
    private TextView Loginmsg;
    public Button Btlogin;
    public static int Counter;
    public static ArrayList <User> mUsers;
    public static int Tester;
    private String nameHolder;
    private String passHolder;






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     Name = findViewById(R.id.etpsudo);
     Password = findViewById(R.id.etpaswword);
     Loginmsg = findViewById(R.id.txtlog);
     Btlogin = findViewById(R.id.btnlogin);
     Tester=0;

     Btlogin.setOnClickListener(new View.OnClickListener() {
         @SuppressLint("SetTextI18n")
         @Override
         public void onClick(View v) {

             nameHolder=Name.getText().toString();
             passHolder=Password.getText().toString();
             for(int i=0; i<=mUsers.size();i++){
                 if((nameHolder.equals(mUsers.get(i).getmNom()))&&(passHolder.equals(mUsers.get(i).getmPass()))){
                     Counter=i;
                     i=mUsers.size()+1;
                     Tester=1;
                 }

             }
             if (Tester==1){
                 Intent drawless = new Intent(MainActivity.this,newacc.class);
                 startActivity(drawless);

             }
             else{
                 Loginmsg.setText("eroor");
             }

         }
     });
     mUsers = new ArrayList<>();
     mUsers.add(new User("amine","12345"));
     mUsers.add(new User("bouhali","4523"));
     mUsers.add(new User("khawla","ae12"));

    }


}
  • You can break out of a `for` loop using `break` instead of modifying the loop counter. Using `<=` in the loop conditional here means that `i` moves beyond the index of the last element. – Dave Newton Oct 18 '21 at 12:42
  • Arrays and lists in Java are indexed from 0 to size_or_length - 1. But if nothing matches, your code will try `mUsers.get(mUsers.size())`. That is one beyond the last element. That will throw an exception. The bug is in the `for` loop's end condition. Read it carefully. – Stephen C Oct 18 '21 at 12:45
  • sorry can you give me an example cause i am still new also what do you mean by" i moves beyond the index of the last element?" – Amine Bouhabinie Oct 18 '21 at 12:49
  • @AmineBouhabinie Say `mUsers` contains 3 elements. `for(int i = 0; i <= 3; i++)` will loop 4 times. It starts with `i = 0`, then `1`, then `2` and then `3`. Trying to find 4 different index in a list that contains 3 elements is going to cause trouble. – Ivar Oct 18 '21 at 12:55
  • thanks dave. and Stephen for helping me it work !!! – Amine Bouhabinie Oct 18 '21 at 13:01

1 Answers1

0

You have a problem in you for loop Look carefully at for(int i=0; i<=mUsers.size();i++).

ArrayLists are index starting at 0. This mean that the last element is less than the total number, not equal to the total number.

Also I agree with Dave, use a break rather than modifying the value of i to end the loop, it is much clearer.

RichardFeynman
  • 478
  • 1
  • 6
  • 16