2016-04-15 42 views
0

非常简单的问题,但很少运气。在另一个类中调用方法时返回null

我的问题是,当我尝试对GET方法getUserIDLoginUsergetPassword打电话,我得到空。

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

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

User类

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

我要访问的用户类的用户ID和密码getter方法在登录类中使用。

登录类

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和密码相匹配。

回答

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

Passing the Employee the the User's constructor is the best solution if you need an instance of Employee to creat a User.

如果你不具备任何Employee例如在您的登录,但只是一个id和a password然后您可以更改用户的构造函数::

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

public class Login 

    private User user; 

    public Login(){ 
    } 

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

}

+0

这使我想到我在改变设计之前遇到的另一个问题。在'User'构造函数中添加一个参数来获取'Employee'对象意味着当我想在'Login'中初始化一个'User'对象时,我会得到构造函数错误:“required:找到的雇员:没有参数”。然而,我不想在'Login'构造函数中使用一个对象,因为我想实现一个带有ID和密码的登录系统。 – AbiAik

+0

好吧,然后检查我的更新解决方案,以使用其ID和密码构建用户。 – Nirekin

+0

在编辑过程中,创建一个'User'对象允许用户设置他们自己的账户信息,但是他们不能自动为他们设置用户ID。编辑设置好像它是我不能拥有的'Login'类。我的'login'中的'id'和'password'来自'User'类。你能告诉我,如果我在我的问题上不清楚吗?我觉得在遇到'Employee'和'User'时,我的问题是'User'和'Login'。谢谢 – AbiAik

0

琴弦userID实例化的Userpassword属于Employee对象。您没有在User课程中初始化这些值,这就是为什么它们是null

你可以做这样的

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

如果你希望他们属于User类,你宣布他们在类的方式,你需要改变你的User构造。例如

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

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

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

+0

'userID'属于'User'对象。 'userID'是通过结合员工类的'staffID'和'authorityLevel'创建的: userID = employee.getID()+“”+ employee.getAuthorityLevel(); – AbiAik

+0

我为我的格式化道歉。当我按Enter键开始一个新行时,它只是提交我的评论。 – AbiAik

相关问题