Y
Y
Yaroslav Kudrin2021-08-14 11:46:46
Java
Yaroslav Kudrin, 2021-08-14 11:46:46

Why is an element missing from an ArrayList?

I have a netty server that accepts mail and password to register a new account, but for some reason after creation, the account disappears without a trace.

Registration package handler:

public void packRegisterRead(ChannelHandlerContext ctx, ByteBuf in) {
        int emailLength = in.readInt();
        int passwordLength = in.readInt();
        ByteBuf emailBuf = in.readBytes(emailLength);
        ByteBuf passwordBuf = in.readBytes(passwordLength);
        String email = StandardCharsets.UTF_8.decode(emailBuf.nioBuffer()).toString(),
                password = StandardCharsets.UTF_8.decode(passwordBuf.nioBuffer()).toString();
        boolean create = true;
        for (int i = 0; i < handler.logic.accounts.size(); i++) {
            System.out.println(handler.logic.accounts.get(i).email + " | " + handler.logic.accounts.get(i).password);
            if (handler.logic.accounts.get(i).email.equals(email)) {
                create = false;
                i = handler.logic.accounts.size();
            }
        }
        if (create) {
            Account account = new Account(ctx, email, password);
            System.out.println(account.email + " | " + account.password);
            handler.logic.accounts.add(account);
            System.out.println(handler.logic.accounts.size() + " accounts");
            account.output.writeBoolean(true);
            UtilsFunctions.write(ctx, account.output, UtilsPackages.PACK_REGISTER);
        }
        else {
            ByteBuf output = ctx.alloc().buffer(1024);
            output.writeBoolean(false);
            UtilsFunctions.write(ctx, output, UtilsPackages.PACK_REGISTER);
        }
        System.out.println(create);
    }

The output in the console after 2 registration attempts with the same mail and passwords and the 1st registration attempt with different mail and password:

Read 4 package, address: /192.168.1.60:59542 , name: ServerHandler#0
yarr | 123
1 accounts
Written 4 package
true
Read 4 package, address: /192.168.1.60:59569 , name: ServerHandler#0
yarr | 123
1 accounts
Written 4 package
true
Read 4 package, address: /192.168.1.60:59606 , name: ServerHandler#0
oleg | 999
1 accounts
Written 4 package
true

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yaroslav Kudrin, 2021-08-20
@Yarru

I already found the answer, I just recreated the list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question