【问题标题】:Returns null when calling method in another class在另一个类中调用方法时返回 null
【发布时间】:2016-08-07 09:21:51
【问题描述】:

很简单的问题,但运气很差。

我的问题是,当我尝试在 Login 类中调用 User 类的 get 方法 getUserIDgetPassword 时,我得到 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!");
    }
}    

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

【问题讨论】:

    标签: java get null return


    【解决方案1】:

    您似乎从未在 User 实例上调用 public void createUser(Employee employee)

    所以

    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);
            }
        }    
        //...
    }
    

    您可以在Login 中实例化User

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

    }

    【讨论】:

    • 这让我想到了我在更改设计之前遇到的另一个问题。在User 构造函数中添加参数以获取Employee 对象意味着当我想在Login 中初始化User 对象时,我会收到构造函数错误:“必需:找到员工:无参数”。但是,我不想在 Login 构造函数中使用对象,因为我想实现一个需要 ID 和密码的登录系统。
    • 好的,然后检查我更新的解决方案以使用其 ID 和密码构建用户。
    • 在编辑中创建User 对象允许用户设置自己的帐户详细信息,但他们不能,因为用户ID 是自动为他们生成的。编辑的设置就像我不能拥有的 Login 类一样。我的login 中的idpassword 来自User 类。如果我不清楚我的问题,你能告诉我吗?我觉得我的问题在于EmployeeUser,而实际上是UserLogin。谢谢
    • 进行了最后一次更新...我不知道您是否清楚,但我知道现在我们离您最初的问题还很远...我确定了您的null 值我给了你两个解决方案来避免它。因此,如果您同意它解决了您的原始问题,请接受此答案。很抱歉,但我无法帮助您完成整个设计。 SO 不是代码工厂...
    【解决方案2】:

    字符串userIDpassword 属于Employee 对象。您没有在 User 类中初始化这些值,这就是为什么它们是 null

    你可以这样做

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

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

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

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

    您应该重新考虑一些设计决策

    【讨论】:

    • userID 属于 User 对象。 userID 是通过组合Employee 类的staffID authorityLevel 创建的:userID = employee.getID() + "" + employee.getAuthorityLevel();
    • 我为我的格式道歉。当我按 Enter 开始新行时,它只会提交我的评论。
    猜你喜欢
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2019-07-15
    • 2018-09-20
    • 2018-05-07
    • 1970-01-01
    相关资源
    最近更新 更多