1

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;
    }

}

2 Answers 2

1

When count is equal to -1, you are trying to access the getcost method of drinks[-1]. I believe this will be fixed if you change "while count >= -1" to "while count > -1".

This will obviously require a small amount of restructuring, for the drink to then be inserted in the correct place.

Sign up to request clarification or add additional context in comments.

8 Comments

I may have been wrong about the restructuring. In this case, when the loop starts with count == 0, it exits with count == -1. If you have the test condition as count >= -1, then it tests true again, and you end up with count == -2. If you just change >= to >, then it should work.
Ah. An array with length of five goes from [0] to [4]. Because you are stepping from i == 1 to i == drinks.length, you are stepping one over the end of the array. It is showing as an error on the while loop line because that is the first place where you are actually accessing the array element. Always step up to < length, rather than <= length.
Final update: last minute discovery lead to me realizing I was a moron. I had created the array with size ten, only instantiated five objects in that size. so I was trying to step into an array to objects that weren't instantiated. Which is why I had created a counter variable that incremented every time I created a new object... apparently I forgot about said counter. So. That fixed it. Thank you :)
Can you check what the values are for count and i when it fails? If you log them as you go, that ought to give you a clue. If it doesn't start at all, check that the objects in the list have actually been created.
I sympathise deeply. Well done for figuring it out! Also, you are welcome. I would appreciate it if you could mark the question as 'solved'.
|
1

Why not implement the comparable interface in your Drink class?

public class Drink implements Comparable<Drink> {

    // attributes and constructor

    public int getCost() {
        return cost;
    }

    public int compareTo(Drink other) {
        return getCost().compareTo(other.getCost());
    }
}

Then later on you can sort the array of Drink objects that is passed with:

Collections.sort(drinks); // returns the sorted drinks

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.