0

i am trying to sort the employee object based on salary using the junit test case.

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

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..

7
  • 1
    Could you please provide a stacktrace? Commented Aug 29, 2012 at 12:54
  • 2
    I suggest you read the NullPointerException to see where it occurred and use your debugger to try to debug the code to determine why this is happening. Commented Aug 29, 2012 at 12:55
  • 1
    Why not use an ArrayList rather than an Array and then use Collections.sort() and use a custom comparator. That would simplify things quite a lot I think Commented Aug 29, 2012 at 12:55
  • 1
    Is this homework? Don't use arrays, use java.util.List (java.util.ArrayList). If you must use arrays, passing a number of elements in the array to your constructor is not necessary since there's a length field on the array objects (you can find out the number of elements from employee.length). Commented Aug 29, 2012 at 12:58
  • 1
    You know, if you want to sort a list of Employees you could just implement a compareTo method that works based on the salaries and then use Arrays.sort(..). Commented Aug 29, 2012 at 13:00

2 Answers 2

7

I suspect this is the line of the 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.

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) };
Sign up to request clarification or add additional context in comments.

2 Comments

I get the answer by doing like this......Employee firstEmployee=new Employee(); firstEmployee.setName("pratap"); firstEmployee.setSalary(10000); firstEmployee.setId("k0100"); employee[0]=firstEmployee; firstEmployee.setSalary(1000); firstEmployee.setId("k0101"); firstEmployee.setName("aruna"); employee[1]=firstEmployee;
Try printing System.out.println(Arrays.toString(employee)); at various points or stepping through the code in your debugger. This will allow you to see what is happening.
0

After

 Employee[] employee = new Employee[5];

for each index in array you need to initialize Employee object.

 employee[0] = new Employee(); etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.