Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:08:17+00:00 2026-06-11T11:08:17+00:00

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:08:18+00:00Added an answer on June 11, 2026 at 11:08 am

    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) };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to sort the employees object array by age and i'm getting the
Trying to sort an array by writing my own sort method using recursion (Pine's
I'm setting up a new store using 1.4.1 I'm trying sort the products in
I am trying to sort a multidimensional JSON array (called jsontest here) using JQuery
I'm trying to sort through a list of 32-bit numbers using MIPS assembler and
Im a beginner on Java and I'm having problem using Comparable to sort Employee
Suppose I have ObservableCollection of employee class public ObservableCollection<Employee> employeeCollection = new ObservableCollection<Employee>(); public
I trying to sort this HashMap based on date in keys My Hash map:
I am trying to sort a dict based on its key and return an
I am trying to sort a list using delegates but I am getting a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.