[英]showing null pointer exception with junit test case
i am trying to sort the employee object based on salary using the junit test case. 我正在尝试使用junit测试用例根据薪水对员工对象进行排序。
it is my employee sort class 这是我的员工分类课
package day4;
import day4.Employee;
public class EmployeesInfoWithSalary {
private Employee[] employee;
private int numberOfEmployees;
public EmployeesInfoWithSalary(Employee[] employee, int numberOfEmployees) {
super();
this.employee = employee;
this.numberOfEmployees = numberOfEmployees;
}
public Employee[] getSortBasedOnSalary() {
String temp;
for (int iterator = 0; iterator < numberOfEmployees; iterator++) {
int minSalary = employee[iterator].getSalary();
int index = iterator;
for (int comparator = iterator; comparator < numberOfEmployees; comparator++) {
if (employee[comparator].getSalary() < minSalary) {
index = comparator;
minSalary = employee[comparator].getSalary();
}
}
employee[index].setSalary(employee[iterator].getSalary());
employee[iterator].setSalary(minSalary);
temp = employee[index].getId();
employee[index].setId(employee[iterator].getId());
employee[iterator].setId(temp);
temp = employee[index].getName();
employee[index].setName(employee[iterator].getName());
employee[iterator].setName(temp);
}
return employee;
}
}
employee object class is as follows 员工对象类如下
package day4;
public class Employee {
private String id;
private String name;
private int salary;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
testemployee salary class for junit test case is as follows junit测试用例的受雇人员薪资等级如下
package day4;
import static org.junit.Assert.*;
import day4.Employee;
import org.junit.Test;
public class TestEmployeeInfoWithSalary {
@Test
public void testGetSortBasedOnSalary() {
Employee[] employee = new Employee[5];
employee[0].setName("pratap");
employee[1].setName("aruna");
employee[2].setName("satyam");
employee[3].setName("krishna");
employee[4].setName("siva");
employee[0].setId("k0100");
employee[1].setId("k0101");
employee[2].setId("k0102");
employee[3].setId("k0103");
employee[4].setId("k0104");
employee[0].setSalary(10000);
employee[1].setSalary(1000);
employee[2].setSalary(8000);
employee[3].setSalary(6000);
employee[4].setSalary(9000);
EmployeesInfoWithSalary employeeInfoWithSalary= new EmployeesInfoWithSalary(employee, 5);
employee[4].setName("pratap");
employee[0].setName("aruna");
employee[2].setName("satyam");
employee[1].setName("krishna");
employee[3].setName("siva");
employee[4].setId("k0100");
employee[0].setId("k0101");
employee[2].setId("k0102");
employee[1].setId("k0103");
employee[3].setId("k0104");
employee[4].setSalary(10000);
employee[0].setSalary(1000);
employee[2].setSalary(8000);
employee[1].setSalary(6000);
employee[3].setSalary(9000);
assertArrayEquals(employee,employeeInfoWithSalary.getSortBasedOnSalary());
}
}
the log is showing the error that null point expression.. 该日志显示了空点表达式的错误。
can any body help me.. thanks.. 谁能帮我..谢谢..
I suspect this is the line of the NPE. 我怀疑这是NPE的路线。
// creates an array full of null values.
Employee[] employee = new Employee[5];
employee[0].setName("pratap");
You need to add Employee objects to each element in the array. 您需要将Employee对象添加到数组中的每个元素。
A better approach is to use a constructor which takes all the needed fields. 更好的方法是使用构造函数,该构造函数接受所有需要的字段。
Employee[] employee = {
new Employee("pratap", "k0100", 10000),
new Employee("aruna", "k0101", 1000),
new Employee("satyam", "k0102", 8000),
new Employee("krishna","k0103", 6000),
new Employee("siva", "k0104", 9000) };
After 后
Employee[] employee = new Employee[5];
for each index in array you need to initialize Employee
object. 对于数组中的每个索引,您需要初始化Employee
对象。
employee[0] = new Employee(); etc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.