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?