【问题标题】:Cannot invoke String.equals(Object) [duplicate]无法调用 String.equals(Object) [重复]
【发布时间】:2022-01-21 01:37:06
【问题描述】:
private void login() throws UserException, NoSuchAlgorithmException, InvalidKeySpecException {
    System.out.println("=== Log in ===");
    while (true) {
        System.out.println("Enter your login \nor Enter EXIT for return to main menu ->");
        String inputLogin = this.sc.nextLine();
        if ("EXIT".equalsIgnoreCase(inputLogin)) {
            break;
        }
        try {
            User user = userImpl.read(inputLogin);
            if (user.getLogin() == null) {
                System.err.println("Username incorrect.");
            } else {
                System.out.println("Enter your password \n or Enter EXIT for return to main menu ->");
                String inputPassword = this.sc.nextLine();
                user = userImpl.readPassword(inputPassword);
                if (!user.getPassword().equals(generateStrongPasswordHash(inputPassword)) || "EXIT".equalsIgnoreCase(inputPassword)) {
                    System.err.println("Password incorrect");
                    continue;
                }
                System.out.println("Log in successfully✔✔✔");
            }
        } catch (UserException | NoSuchAlgorithmException | InvalidKeySpecException e) {
            e.printStackTrace();
        }
    }
}

我正在尝试使用 JDBC (MySQL) 实现控制台程序 Cinema。使用登录方法后,我得到了这个Exception。 如何在控制台应用上实现授权? User User

【问题讨论】:

  • 可能是 readPassword 方法有问题? user = userImpl.readPassword(inputPassword);

标签: java sql jdbc null console-application


【解决方案1】:

似乎user.getPassword() 正在返回null。因此,我建议您在检查 user.getPassword().equals(generateStrongPasswordHash(inputPassword)) 之前检查 user.getPassword() === null

private void login() throws UserException, NoSuchAlgorithmException, InvalidKeySpecException {
    System.out.println("=== Log in ===");

    while (true) {
        System.out.println("Enter your login \nor Enter EXIT for return to main menu ->");
        String inputLogin = this.sc.nextLine();

        if ("EXIT".equalsIgnoreCase(inputLogin)) {
            break;
        }

        try {
            User user = userImpl.read(inputLogin);

            if (user.getLogin() == null) {
                System.err.println("Username incorrect.");
            } else {
                System.out.println("Enter your password \n or Enter EXIT for return to main menu ->");
                String inputPassword = this.sc.nextLine();
                user = userImpl.readPassword(inputPassword);

                if (user.getPassword() === null || !user.getPassword().equals(generateStrongPasswordHash(inputPassword)) || "EXIT".equalsIgnoreCase(inputPassword)) {
                    System.err.println("Password incorrect");
                    continue;
                }

                System.out.println("Log in successfully✔✔✔");
            }
        } catch (UserException | NoSuchAlgorithmException | InvalidKeySpecException e) {
             e.printStackTrace();
        }
    }
}

【讨论】:

  • 错误信息显示user.getPassword() 为空,而不是user
  • 你是对的。我会调整我的答案。
  • 我尝试使用您的方法,但现在它显示“密码不正确”,即使我输入了有效密码
  • 我不知道你的 User 类的实现,因此我不知道 getPassword() 在做什么。
  • @Gregor 没有密码散列一切正常,但散列却不行
猜你喜欢
  • 2020-10-30
  • 2011-08-13
  • 2014-04-18
  • 2012-04-27
  • 2015-08-02
  • 1970-01-01
  • 2012-11-17
  • 1970-01-01
相关资源
最近更新 更多