【问题标题】:Why does my method return a null password?为什么我的方法返回空密码?
【发布时间】:2009-11-20 14:52:33
【问题描述】:

我有两个类:一个 Generator 类和一个 SystemManagement 类。 Generator 类,我可以生成密码。 SystemManagement 类导入 Generator(来自另一个包)并包含其他信息。

当我创建一个 SystemManagement 对象并对其调用 getPassword() 时,我会返回 null。为什么会这样?

生成器:

public class Generator {

    private static String password;
    private static Generator instance;

    public String nextPassword() {
        char[] allowedCharacters = {'a', 'b', 'c', 'd', 'e', 'f', '1', '2', '3', '4',   '5','6'};

        SecureRandom random = new SecureRandom();
        StringBuffer password1 = new StringBuffer();
        for (int i = 0; i < password1.length(); i++) {
            password1.append(allowedCharacters[random.nextInt(allowedCharacters.length)]);
        }
        password = password1.toString();
        return password;


    }

    public String currentPassword() {
        return password;
    }

    public static void setPassword(String password) {
        Generator.password = password;
    }

    public static Generator getInstance() {
        if (instance == null) {
            instance = new Generator();
        }
        return instance;
    }
}

系统管理:

public class SystemManagement implements Serializable {
    private String name;
    private String family;
    private String password;

    public SystemManagement() {

    }

    public SystemManagement(String password) {
        this.password = Generator.getInstance().nextPassword();
    }

    public Students addStudent(String name, String family) {
        Students student = new Students(name, family);
        students.add(student);
        return student;

    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

【问题讨论】:

标签: java null passwords


【解决方案1】:

我可以发现您的代码存在一些问题:

  • 您的 nextPassword() 方法总是返回一个空的 String 作为密码,因为您遍历了一个长度为 0 的空 StringBuilder

  • 当您使用无参数构造函数创建一个新的SystemManagement 对象时,您的password 为空,因为您没有为其分配任何内容(除非您使用setPassword 设置器)。

  • 当您使用采用String 的构造函数创建新的SystemManagement 对象时,您将忽略该参数并且您的password 为空,因为nextPassword() 总是返回一个空的String

【讨论】:

    【解决方案2】:

    password1.length() 第一次是0,所以永远不会被填满?

    【讨论】:

    • 这将返回一个空字符串,而不是 null。
    【解决方案3】:
     StringBuffer password1 = new StringBuffer();
        for (int i = 0; i < password1.length(); i++) {
    

    我想那将是0 用于字符串缓冲区的length

    【讨论】:

      【解决方案4】:

      如果您调用不带参数的构造函数(如sys = new SystemManagement();),则永远不会设置新对象的password 成员。仅当您调用采用 String 的构造函数时才会发生这种情况 - 您忽略它。

      【讨论】:

        【解决方案5】:
        StringBuffer password1 = new StringBuffer();
        for (int i = 0; i < password1.length(); i++) {
            password1.append(allowedCharacters[random.nextInt(allowedCharacters.length)]);
        }
        

        for 循环永远不会运行,因为此时password1.length() 将为 0。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-25
          • 1970-01-01
          • 1970-01-01
          • 2015-01-02
          • 1970-01-01
          • 1970-01-01
          • 2018-02-11
          • 2015-01-09
          相关资源
          最近更新 更多