2013-03-18 106 views
0

当我将getters和setter放入方法中,然后在main方法中调用该方法时,尽管将该值设置为其他值,我仍然得到null值!另外,我没有收到编译器的任何错误,所以我确定这是一个逻辑错误,但我无法弄清楚。getter和setter变为空

这是包含问题方法的类:

public class URTest { 
UserRegistration _user = new UserRegistration(); 
public static void main(String[] args) { 
    String fieldName; 
    UserRegistration _user = new UserRegistration(); 
    URTest _run = new URTest(); 
    Scanner input = new Scanner(System.in); 
    fieldName = input.nextLine(); 
    _run.test(fieldName); 
    System.out.println(_user.getUsername()); 
} 

public void test(String fieldName) { 
    UserRegistration _user = new UserRegistration(); 
    Scanner input = new Scanner(System.in); 
    String result; 
    System.out.print("Enter " + fieldName + ": "); 
    result = input.nextLine(); 
     while(true) { 
      if(result.equals("")) { 
       System.out.print("Please enter a value for the " + fieldName + ": "); 
       result = input.nextLine(); 
      } else { 
       break; 
      } 
      if(fieldName.equals("username")) { 
       _user.setUsername(result); 
      } 
} 

} }

这里是类包含我的getter和setter方法:

import java.io.Serializable; 

public class UserRegistration implements Serializable { 

private String username; 
private int userId; 
private String firstName; 
private String lastName; 
private String password; 
private String email; 

public UserRegistration() { 
    this.username = username; 
    this.userId = userId; 
} 

public UserRegistration(String uName, int id, String uPassword) { 
    this.username = uName; 
    this.userId = id; 
    this.password = uPassword; 
} 

public String getUsername() { 
    return username; 
} 

public void setUsername(String username) { 
    this.username = username; 
} 

public int getUserId() { 
    return userId; 
} 

public void setUserId(int userId) { 
    this.userId = userId; 
} 

public String getFirstName() { 
    return firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

public String getLastName() { 
    return lastName; 
} 

public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

@Override 
public String toString() { 
    String name = getFirstName() + getLastName(); 
    String output = name + "has the user id " + getUserId() + 
      "username " + getUsername() + " and email " + getEmail(); 
    return output; 
} 

}

回答

4

您正在致电

​​

而在你的类

public UserRegistration() { 
    this.username = username; 
    this.userId = userId; 
} 

当上述构造函数将被调用,没有被设置为成员变量的值,因此你越来越空

在你调用的方法,

UserRegistration _user = new UserRegistration("name","id1"); 

OR

在您的构造函数中设置默认值:

public UserRegistration() { 
     this.username = "name"; 
     this.userId = "id1"; 
    } 
+0

谢谢,我看到我出错了,并认为我知道如何解决问题! – 2013-03-18 01:54:21

+3

@CuckoOooo - 如果有帮助,请接受答案 – Abi 2013-03-18 01:59:18

+0

@CuckoOooo请注意,考虑链接构造函数:http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another -in-java的 – tjameson 2013-03-18 02:13:51