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