English 中文(简体)
Why does my method return a null password?
原标题:

I have two classes: a Generator class and a SystemManagement class. The Generator class, I can generate a password. The SystemManagement class imports Generator (from another package) and includes additional information.

When I make a SystemManagement object and call getPassword() on it, I get back null. Why is this happening?

Generator:

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;
    }
}

SystemManagement:

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;
    }
}
问题回答

I can spot a couple of problems with your code:

  • Your nextPassword() method always returns an empty String as the password because you iterate over an empty StringBuilder with length 0.

  • When you create a new SystemManagement object with the parameter-less constructor, your password is null because you assign nothing to it (unless you use the setPassword setter).

  • When you create a new SystemManagement object with the constructor that takes a String, you ignore the parameter and your password is empty because nextPassword() always returns an empty String.

password1.length() is 0 first time, so it never gets filled?

 StringBuffer password1 = new StringBuffer();
    for (int i = 0; i < password1.length(); i++) {

I would imagine that that would be 0 for the length of the string buffer.

If you call the constructor with no arguments (like sys = new SystemManagement();), the new object s password member is never set. That only happens if you call the constructor that takes a String—which you ignore.

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

The for loop will never run as password1.length() will be 0 at this time.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签