This insertion sort function is supposed to take in an array of Drink objects and sort them according to one of their properties (cost). This property is fetched by getCost(). I keep getting a NullPointer error. The code is as follows:
public void sortDrinks(Drink[] drinks){
for(int i = 1; i <= drinks.length; i++){
Drink key = drinks[i];
int count = i-1;
while((count >= -1)&&(drinks[count].getCost() > key.getCost())){
drinks[count+1] = drinks[count];
count--;
}
drinks[count+1] = key;
}
}