简体   繁体   中英

Getting null with getters and setters

When I place the getters and setters in a method, then call that method in the main method, I get a value of null despite having set the value to something else! Additionally, I'm not receiving any errors from the compiler, so I'm sure it's a logical error somewhere but I cannot figure it out.

This is from the class containing the methods in question:

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

} }

Here is the class containing my getters and setters:

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

}

You are calling

UserRegistration _user = new UserRegistration();

And in your class

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

When the above constructor gets called, there is no value to be set to the member variable and therefore you are getting null

In your calling method,

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

OR

set default values in your constructor:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM