Johanna
Johanna

Reputation: 27660

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

Upvotes: 0

Views: 320

Answers (5)

willcodejavaforfood
willcodejavaforfood

Reputation: 44073

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.

Upvotes: 0

bruno conde
bruno conde

Reputation: 48265

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.

Upvotes: 3

erickson
erickson

Reputation: 269797

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.

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190966

 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.

Upvotes: 0

Ariel
Ariel

Reputation: 5830

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

Upvotes: 0

Related Questions