0

I try to create a program where the user can create multiple accounts and save the data in arrays (user and pas), but i keep getting an 'ArrayIndexOutOfBoundsException' error. It's probably a stupid mistake i made, does someone sees it?

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.PasswordField ;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Main extends Application {

    Button btnLogin,btnAanmaken;
    TextField txtUsername;
    PasswordField Password;
    Label lblUsername,lblPassword;
    int index;
    String[] user = new String[10];
    String[] pas = new String[10];
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Aanmelden");
        btnLogin = new Button();
        btnLogin.setText("Login");

        btnAanmaken = new Button();
        btnAanmaken.setText("Aanmaken");

        txtUsername=new TextField();
        txtUsername.setMaxWidth(200);

        Password=new PasswordField();
        Password.setMaxWidth(200);

        lblUsername=new Label();
        lblUsername.setText("Gebruikersnaam");

        lblPassword=new Label();
        lblPassword.setText("Paswoord");

        btnLogin.setOnAction(e -> {
               if (txtUsername.getText().equals("user") && Password.getText().equals("pass")){
                   AlertBox.display("Welkom", "U bent nu ingelogd");
                }
                   else{
                   AlertBox.display("Fout ", "De opgegeven gebruikersnaam of het opgegeven wachtwoord is onjuist."); 
                    }
            });

            btnAanmaken.setOnAction(e ->{
                index++;
                user[index]=txtUsername.getText();
                pas[index]=txtUsername.getText();
                System.out.println(pas[index]);
            });


        VBox layout = new VBox();
        layout.getChildren().addAll(lblUsername,txtUsername,lblPassword,Password,btnAanmaken,btnLogin);
        Scene scene = new Scene(layout, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
petie
  • 77
  • 3
  • 13

1 Answers1

1

you should increase index after using it:

            user[index]=txtUsername.getText();
            pas[index]=txtUsername.getText();
            index++;

in your case index 0 is never set in your arrays and that is the reason I think why you get an exception for index 10.

well... I think you need additional a check in you condition if the size is already reached or you should use a List:

    btnAanmaken.setOnAction(e ->{
       if (index < 10){
            index++;
            user[index]=txtUsername.getText();
            pas[index]=txtUsername.getText();
            System.out.println(pas[index]);
       }else{
          System.out.println("the arrays reaches the max size!");
       } 
       });
nano_nano
  • 12,351
  • 8
  • 55
  • 83