The RemoveMin class should remove the first occurrence of the smallest element from the input list. Despite what the inputs are I keep getting the same NullReference error from having 'rem = null'. I don't know if I'm using recursion right or not. I have a feeling the starting reference for the ConsList may not be right. This was a homework assignment but it was due a couple days ago. I just want to understand for future references what I need to do. Anything pointers or examples would be appreciated. Here is the line of code giving me problems.

public static ConsList<int> RemoveMin(ConsList<int> list, out int min)
        {
            ConsList<int> r;
            min = list.Head;
                if (list.Tail.Head > min)
                {
                    r = new ConsList<int>(list.Head, RemoveMin(list.Tail, out min));
                    return r;
                }
                else
                {
                    r = new ConsList<int>(min, RemoveMin(list.Tail, out min));
                    return r;
                }
        }

Here is the class that implements RemoveMin. 'rem' keeps causing a null reference. This class shouldn't need any editing.

private ConsList<int> SelectionSort(ConsList<int> list)
        {
            if (list == null)
            {
                return list;
            }
            else
            {
                int min;
                ConsList<int> rem = RemoveMin(list, out min);
                return new ConsList<int>(min, SelectionSort(rem));
            }
        }

Recommended Answers

All 2 Replies

Care to post the full code unit so I can load it up in a debugger and test without putting it back together?

The problem might be in the creation of the object.
Passing a recursive parameter can be a trouble, becouse every single call to the function will create a different object an you're using a type out value wich has no value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.