JasonL95
JasonL95

Reputation: 97

Exception error in Java while attempting sorting

i am doing sorts in java and i have ran into an error. The error is stemming from my class file and then the tester i have in turn, wont work. Here is the class

import java.util.*;

public class CompanyDataBase
{
    ArrayList<Employee> employee = new ArrayList<Employee>();
}

//Default Constructor
public CompanyDataBase()
{

}

//Methods
//Add Employee
public void addEmployee(Employee newGuy)
{
   employee.add(newGuy);
}

//Get the employees
public ArrayList<Employee> getEmployees()
{
   return employee;
}

//Sort by name
public ArrayList<Employee> sortByName()
{
   Collections.sort(employee, new EmployeeNameComparator());
   return employee;
}

//Sort by salary
public ArrayList<Employee> sortBySalary()
{
   Collections.sort(employee, new EmployeeSalaryComparator());
   return employee;
}

//Bubble Sort
public void bubblesortBySalary(int [] employee)
{
    int temp = 0;
    boolean arraySorted = false;

    for (int i = 0; i < employee.length - 1 && !arraySorted;
                            i++ )
    {
    arraySorted = true;   // start a new iteration;
                          //  maybe the array is sorted

    for ( int j = 0; j < employee.length - i - 1; j++ )
    {
       if ( employee[j] > employee[j + 1] )
       {
          // swap the adjacent elements
          //   and set arraySorted to false
          temp = employee[j + 1];
          employee[j + 1] = employee[j];
          employee[j] = temp;
          arraySorted = false;
       }
    }
 }

}

}

Also here is the tester that i am using

public class BubbleSortTester
{
    public static void main(String[] args)
    {
        CompanyDataBase myDb = new CompanyDataBase();

        myDb.addEmployee(new Employee("James, John", 34, 45234));
        myDb.addEmployee(new Employee("Conroy, Mike", 19, 19234));
        myDb.addEmployee(new Employee("Gavigan, Luke", 28, 30234));
        myDb.addEmployee(new Manager("Gates, Bill", 28, 44000, 2000));

        myDb.bubblesortBySalary();


        for(Employee currEmployee: myDb.getEmployees())
        {
            System.out.println(currEmployee.getDescription());
        }

    }
}    

The problem i am having is that when i attempt to compile my tester it gives me the following error >>>>
BubbleSortTester.java:16: error: method bubblesortBySalary in class CompanyDataBase cannot be applied to given types; myDb.bubblesortBySalary(); ^ required: int[] found: no arguments reason: actual and formal argument lists differ in length 1 error

I then tried it in Eclipse instead of jGrasp and that advised me to enter null but when i enter null it returns this error >>> Exception in thread "main" java.lang.NullPointerException at CompanyDataBase.bubblesortBySalary(CompanyDataBase.java:46) at BubbleSortTester.main(BubbleSortTester.java:16)

Sorry for the amount of code but any help would be appreciated, Thanks in advance, Jason

Upvotes: 0

Views: 134

Answers (2)

Richard Tingle
Richard Tingle

Reputation: 17226

You have called bubblesortBySalary as:

myDb.bubblesortBySalary();

but your definition of the method is

public void bubblesortBySalary(int[] employee){
   ....
}

So the method was expecting an int[] but didn't recieve one

Possible solution

You method appears to expect an array of integers to sort which it calls employees, you also have an instance field called employees which I believe you may be intending to sort. If so you will need to provide some method to compare Employees and remove the passed int[]. If you just want to sort integers you need to provide those integers to the method

Upvotes: 2

musical_coder
musical_coder

Reputation: 3896

The function bubblesortBySalary(); requires an argument of type int[]. You need to supply that for this to compile.

Passing null makes the compiler happy, but doesn't solve the fundamental problem that you haven't supplied an array to be sorted. Once you define the array, such as int[] employee = new int[] {1, 2, 3, 4}, pass it to the function as myDb.bubblesortBySalary(employee);.

Upvotes: 2

Related Questions