0

I have got a Bean named "EmployeeModel" and I also got another Class "EmployeeManager" which has one method of removing (deleting) an employee.

My EmployeeModel:

    package at.fh.swenga.employee.model;

import java.util.Date;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;

import org.springframework.format.annotation.DateTimeFormat;

public class EmployeeModel implements Comparable<EmployeeModel> {

    @Min(1)
    private int ssn;

    private String firstName;
    private String lastName;

    private int salary;


    @NotNull(message = "{0} is required")
    @DateTimeFormat(pattern = "dd.MM.yyyy")
    @Past(message = "{0} must be in the past")
    private Date dayOfBirth;

    public EmployeeModel() {
    }


    public EmployeeModel(int ssn, String firstName, String lastName, Integer salary,
            Date dayOfBirth) {
        super();
        this.ssn = ssn;
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
        this.dayOfBirth = dayOfBirth;
    }

    public int getSsn() {
        return ssn;
    }

    public void setSsn(int ssn) {
        this.ssn = ssn;
    }

    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 int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public Date getDayOfBirth() {
        return dayOfBirth;
    }

    public void setDayOfBirth(Date dayOfBirth) {
        this.dayOfBirth = dayOfBirth;
    }

    @Override
    public int compareTo(EmployeeModel o) {
        return ssn - o.getSsn();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ssn;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        EmployeeModel other = (EmployeeModel) obj;
        if (ssn != other.ssn)
            return false;
        return true;
    }
}

My Method in the EmployeeManager Class:

public boolean remove(int ssn) {
    return employees.remove(new EmployeeModel(ssn, null, null,null, null));
}

As you can see the method only takes the ssn which is of type "int". The Problem is when my constructor takes the salary as an int I have to provide it in the method too but I want to avoid this. As you can see my constructor has an wrapper Integer at the salary field but whenever I am starting my Application and trying to remove an Employee I am getting NullPointerExceptions?

My question is now why I am getting them and if I should just use also a wrapper class at the instantiation like "private Integer salary;" instead of using the primitve way?

bajro
  • 1,199
  • 3
  • 20
  • 33

1 Answers1

0

1) You should be getting a validation error when calling:

new EmployeeModel(ssn, null, null,null, null)

The last parameter is the dayOfBirth, which you have a @NotNull validation check.

2) If you find yourself calling a constructor like this with null parameters, then you likely have an issue, either with the required parameters needed to construct the object (Should ssn be the only required parameter), or you don't have enough valid data to properly instantiate the object. If the dateOfBirth should be not null, then you should not be passing null to the constructor.

Maybe you should remove the default constructor EmployeeModel() and replace it with one that expects the minimal parameters, like:

public EmployeeModel(int ssn, Date dayOfBirth)

You shouldn't try to 'fake' the data just to overcome an issue like this. From looking at your code, it seems that SSN is the only real data that must be present to create an EmployeeModel, so maybe you should remove the @NotNull from the dayOfBirth and provide a single-argument constructor that accepts only the ssn. That should solve your problem. You will want to insure anywhere you do calculations, like on the salary, that you do defensive null checks before attempting the calculation.

public EmployeeModel(int ssn){
    this.ssn = ssn;
}
pczeus
  • 7,709
  • 4
  • 36
  • 51
  • My question was not about my dayOfBirth, this just works fine. Just please try to read my question again. And yes the ssn is the only attribute which is needed in order to remove an Employee and I didnt want to also pass the attribute salary in order to use the method – bajro May 05 '16 at 14:38
  • I commented on dayOfBirth, but focused on your question. Please try to read and understand the answer again. – pczeus May 05 '16 at 14:40
  • In addition to that, you did not add relevant code demonstrating how `employees` is constructed and used. So any further information would be a 'guess' only. Please read: http://stackoverflow.com/help/mcve on information for creating a minimal, complete, and valid question. – pczeus May 05 '16 at 14:43