如何在pojo中设置类对象的值?

时间:2019-05-07 20:09:37

标签: java spring pojo

我正在尝试在pojo中设置类值,但在Spring Boot服务层中显示了空指针异常 我正在尝试设置vl.setRegister(),但是在这里显示错误“空指针异常”,如何将mongodb用于数据库。下面我在这里提到了一些代码

这是方法

public ValidLogins logins(LoginClient client) {
        // TODO Auto-generated method stub
        ValidLogins vl = new ValidLogins();
        System.out.println("step 1");
        Register r = registryDao.LoginVerify(client.getUsername(), client.getPassword(), client.getRole());
        System.out.println(r.toString());
        vl.setRegister(r);   // null pointer exception error here 
        System.out.println("step 2");
        System.out.println("step 3");
        Register rj = vl.getRegister();
        if(vl.getRegister() != null) {
            System.out.println("step 4");
            if(rj.getStatus().equals("A")) {
                System.out.println("step 5");
                vl.setStatus(true);
                vl.setMsg("Success");
                System.out.println("step 6");
            }else {
                System.out.println("step 7");
                vl.setRegister(null);
                System.out.println("step 8");
                vl.setStatus(false);
                vl.setMsg("User Has been Blocked");
            }
        } 

        return vl;
    }

pojo ValidLogins.java

public class ValidLogins {

    private Register register;

    private Boolean status;

    private String msg;

    public Register getRegister() {
        return register;
    }

    public void setRegister(Register register) {
        this.register = register;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public ValidLogins(Register register, Boolean status, String msg) {
        super();
        this.register = register;
        this.status = status;
        this.msg = msg;
    }

    public ValidLogins() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public String toString() {
        return "ValidLogins [register=" + register + ", status=" + status + ", msg=" + msg
                + "]";
    }


}

pojo Register.java

@Document(collection = "Registrys")
public class Register {

    @Id
    private long id;

    private String name;

    private String username;

    private String password;

    private String role;

    @JsonFormat(pattern="yyyy-MM-dd")
    private Date dob;

    private String branch_name;

    private String status;

    private String address;

    private String contact_no;

    private String email_id;

    private long created_by;

    @JsonFormat(pattern="yyyy-MM-dd")
    private Date created_on;

    private long updated_by;

    @JsonFormat(pattern="yyyy-MM-dd")
    private Date updated_on;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getBranch_name() {
        return branch_name;
    }

    public void setBranch_name(String branch_name) {
        this.branch_name = branch_name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getContact_no() {
        return contact_no;
    }

    public void setContact_no(String contact_no) {
        this.contact_no = contact_no;
    }

    public String getEmail_id() {
        return email_id;
    }

    public void setEmail_id(String email_id) {
        this.email_id = email_id;
    }

    public long getCreated_by() {
        return created_by;
    }

    public void setCreated_by(long created_by) {
        this.created_by = created_by;
    }

    public Date getCreated_on() {
        return created_on;
    }

    public void setCreated_on(Date created_on) {
        this.created_on = created_on;
    }

    public long getUpdated_by() {
        return updated_by;
    }

    public void setUpdated_by(long updated_by) {
        this.updated_by = updated_by;
    }

    public Date getUpdated_on() {
        return updated_on;
    }

    public void setUpdated_on(Date updated_on) {
        this.updated_on = updated_on;
    }

    public Register(long id, String name, String username, String password, String role, Date dob, String branch_name,
            String status, String address, String contact_no, String email_id, long created_by, Date created_on,
            long updated_by, Date updated_on) {
        super();
        this.id = id;
        this.name = name;
        this.username = username;
        this.password = password;
        this.role = role;
        this.dob = dob;
        this.branch_name = branch_name;
        this.status = status;
        this.address = address;
        this.contact_no = contact_no;
        this.email_id = email_id;
        this.created_by = created_by;
        this.created_on = created_on;
        this.updated_by = updated_by;
        this.updated_on = updated_on;
    }

    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public String toString() {
        return "Register [id=" + id + ", name=" + name + ", username=" + username + ", password=" + password + ", role="
                + role + ", dob=" + dob + ", branch_name=" + branch_name + ", status=" + status + ", address=" + address
                + ", contact_no=" + contact_no + ", email_id=" + email_id + ", created_by=" + created_by
                + ", created_on=" + created_on + ", updated_by=" + updated_by + ", updated_on=" + updated_on + "]";
    }
}

0 个答案:

没有答案