I have the int cannot be dereferenced error in the below code where I have //error is here. I’m confused because the variable b is used to reference a spot in the empl array later in the line without showing as an error. So how do I fix this and why is it generating an error?
I would appreciate any help. Example code would be great too as that seems to be the way I learn best. Thanks!
public static void bubbleSort(Employee[] empl) {
for (int a = 1; a < empl.length; a++)
{
for (int b = 0; b < empl.length - a; b++)
{
if (((empl[b].//error is here
getEmployeeNumber()).compareTo
((empl[b + 1].getEmployeeNumber()))) > 0)
{
// swap employees[b] with employees[b+1]
Employee temp = empl[b];
empl[b] = empl[b + 1];
empl[b + 1] = temp;
}
}
}
}
Edit: Any other suggestions for sorting the array by Employee Number are welcome.
getEmployeeNumber()apparently returns anint.intis a primitive type, not an object.Therefore, you cannot call methods like
compareTo()on it.