在另一个类

时间:2016-04-15 11:57:53

标签: java get null return

非常简单的问题,但运气很少。

我的问题是,当我尝试在类getUserID中调用类getPassword的get UserLogin时,我得到null。

我看过其他与我有类似问题的帖子。虽然我发现很少有帖子可以指导我,但是一个常见的问题是海报没有初始化类对象,但我已经完成了。

我在这里需要特定的代码,因为我已经尝试了很多东西,但无法让它发挥作用。我希望我提供足够的信息来帮助你。

用户类

public class User 
{
private String userID;
private String password;
private Employee employee;
private String authorityLevel;
/**
 * Constructor for User class - Initialise a fixed password and employee object.
 */
public User()
{  
    employee = new Employee();
    password = "password";
}    

/**
 * Create a user ID and print the user the details of their user account.
 */
public void createUser(Employee employee)
{  
    // Combine staff ID with authority key to make the user ID.
    userID = employee.getID() + "" + employee.getAuthorityLevel();

    // Check that there is a staff ID to create the user ID.
    // It also ensures that an employee profile has been created before an attempt
    // to make a user account.
    if(employee.getID() == null){
        System.out.println("There are no Employee details to make a User with.");
        System.out.println("Please enter the Employee details before you make a user");
    }
    else{
        System.out.println("Your user ID is: "+userID);
        System.out.println("Your user password is: "+password);
    }
}    

/**
 * @return The user ID.
 */
public String getUserID()
{
    return userID;
}

/**
 * @return The password.
 */
public String getPassword()
{
    return password;
}  
}

我想访问User类中的userID和password getter方法,以便在Login类中使用。

登录类

public class Login
{
private User user;  
private boolean accessGranted;
private String userID;
private String password;   
private boolean loggedIn;
private boolean loggedOut;
/**
 * Constructor for the Login class - initialise a user object.
 */
public Login()
{   
    user = new User();
}   

/**
 * Attempt to start a login session.
 */
public void login(String userID,String password)
{   
    // Check that credentials entered are correct for the account the user wishes to log in to.
    if((password == user.getPassword()) && (userID == user.getUserID())){
        accessGranted = true;
        if((accessGranted == true) && (userID.contains("H"))){
            System.out.println("Your login session has started.");
            System.out.println("You are now viewing Yuconz System as HR staff.");
        }
        if((accessGranted == true) && (userID.contains("D"))){
            System.out.println("Your login session has started.");
            System.out.println("You are now viewing Yuconz System as Director staff.");
        }
        if((accessGranted == true) && (userID.contains("E"))){
            System.out.println("Your login session has started.");
            System.out.println("You are now viewing Yuconz System as Employee staff.");
        }
        loggedIn = true;
    }
    else{
        System.out.println("ACCESS DENIED BRUTHA!");
    }
}    

一个类创建用户帐户,其中唯一的详细信息是userID和密码。另一个类是用户可以使用该帐户的详细信息登录的地方。在Login login方法中,我检查输入的ID和密码是否与他们尝试访问的帐户的ID和密码相匹配。

2 个答案:

答案 0 :(得分:1)

您似乎永远不会在User实例上调用public void createUser(Employee employee)

SO

private String userID;从未初始化...

试试这个:

public class User{

    private String userID;
    private String password;
    private Employee employee;
    private String authorityLevel;

    public User(Employee employee){  
        this.employee = employee;
        password = "password";
        createUser();
    }    

    private void createUser(){  
        userID = employee.getID() + "" + employee.getAuthorityLevel();

        if(userID == null){
            System.out.println("There are no Employee details to make a User with.");
            System.out.println("Please enter the Employee details before you make a user");
        }else{
            System.out.println("Your user ID is: "+userID);
            System.out.println("Your user password is: "+password);
        }
    }    

    public String getUserID(){
        return userID;
    }

    public String getPassword(){
        return password;
    }  
}
  

如果您需要Employee实例来创建用户,那么将Employee传递给User的构造函数是最好的解决方案。

如果您的登录信息中没有Employee的任何实例,只有idpassword,那么您可以更改用户的构造函数::

public class User{
    private String userID;
    private String password;
    private String authorityLevel;

    public User(String userID, String password){  
        this.userID = userID;
        this.password = password;       
        checkUser();
    }    

    private void checkUser(){  
        if(userID == null){
            System.out.println("There are no Employee details to make a User with.");
            System.out.println("Please enter the Employee details before you make a user");
        }else{
            System.out.println("Your user ID is: " + userID);
            System.out.println("Your user password is: " + password);
        }
    }    
    //...
}

您可以在User

中实现Login
public class Login

    private User user;

    public Login(){   
    }   

    public void login(String userID,String password){   
        user = new User(userID, password);
        //...

}

答案 1 :(得分:0)

字符串userIDpassword属于Employee对象。您没有在User课程中初始化这些值,这就是null的原因。

你可以这样做

public String getUerID() {
    return employee.getUserID();

如果您希望它们按照您在类中声明它们的方式属于User类,则需要更改User构造函数。 e.g。

public User(String id, String pwd) {
    this.userID = id;
    this.password = pwd;
}

然后你需要弄清楚你是否还想要Employee对象。

你应该重新考虑一些设计决定