Error "Concurrentmodificationexception" when modifying (insert data into) Arraylist

Asked

Viewed 442 times

0

I am trying to create a small login system in Java.

I have the following classes:

Logincontroller:

public static void main(String[] args) {

    ArrayList<Usuario> listaUsuarios = UsuarioController.listaUsuarios;
    // Adiciona um administrador padrao ao ArrayList de Usuarios
    Usuario adminpadrao = new Usuario("Admin", "123456", 1);
    UsuarioController.listaUsuarios.add(adminpadrao);

    String opcao = "";

    while (!opcao.equals("0")) {

        LoginView lv = new LoginView();
        AdminController ac = new AdminController();
        UsuarioController uc = new UsuarioController();

        Usuario u = lv.capturarLogin();
        // Captura o usuario para fazer o login, verifica se ele esta presente no ArrayList e se ele é um administrador. Caso sim, ele vai para o menu de administrador, Caso não, ele vai para o menu de usuario comum
        for (Usuario usuario : listaUsuarios) {
            if (usuario.getUsuario().equals(u.getUsuario()) && usuario.getSenha().equals(u.getSenha())) {
                if (usuario.getAdmin() == 1) {
                    ac.menuAdmin();
                } else {
                    uc.menuUsuario();
                }
            }
        }

    }
}

Admincontroller:

public class AdminController {

    Usuario user = new Usuario();
    AdminView av = new AdminView();

    ArrayList<Usuario> listaUsuarios = UsuarioController.listaUsuarios;

    public void menuAdmin() {

        String opcao = av.exibirMenuAdmin();

        if (opcao.equals("1")) {
            cadastrarUsuario();
        } else if (opcao.equals("2")) {
            listarUsuarios();
        } else if (opcao.equals("3")) {
            excluirUsuario();
        } else if (opcao.equals("4")) {
            Util.continuar();
        }
    }

    public void cadastrarUsuario() {
        Usuario u = av.capturarUsuario();
        listaUsuarios.add(u);
        Util.continuar("Usuario cadastrado com sucesso");
    }

    public void listarUsuarios() {
        av.listarUsuarios(listaUsuarios);
        Util.continuar("Pressione enter para continuar");
    }

    public void excluirUsuario() {
        String u = av.capturarNomeUsuario();

        for (Usuario usuario : listaUsuarios) {
            if (usuario.getUsuario().equals(u)) {
                Util.escreva("Usuario excluido: " + usuario.getUsuario());
                listaUsuarios.remove(usuario);
            } else {
                Util.continuar("Usuario não encontrado");
            }
        }
}

However, after I log in with the default administrator and try to register a new user, the compiler (Eclipse) returns the error:

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at crm.controller.LoginController.main(LoginController.java:27)

That last line of error at crm.controller.LoginController.main(LoginController.java:27) refers to line for (Usuario usuario : listaUsuarios) { in Logincontroller in login verification step

I think it’s some mistake in Arraylist. I’m trying to use a single Arraylist for the two classes.

I wanted help to resolve this error. I thank you from now on.

  • 2

    This exception usually occurs when you try to modify a list at the same time as you try to scan it with a loop. But it is not very clear why the error in this code only. Gives a read at this link, it explains the main causes of this exception and how to solve.

  • 1

    The problem is exactly what Articuno said. Inside this is called the Admin menu that can delete a user from the list while the same is being iterated, generating the exception.

  • I made some changes to the code and it worked. I created a separate method to check the user/password with the Arraylist data, , which returns a variable of type boolean. Instead of calling the menus through the for, I made a if in the main, depending on the outcome of the verification (trueor false), it leads to the respective menu. Thanks for the help.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.