Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping List.Add() gives - Object reference not set to an instance of an object

I want to wrap List class with my custom class. As for now I have something like this ;

public class PriorityListOfNodes
{

    private List<Node>          list_;
    private IComparer<Node>     sorter_;

    public List<Node> List_ {
            get {return list_;}
            set {list_ = value;}
    }

    public PriorityListOfNodes ()
    {
            sorter_ = new NodeSorter_fValueDescending ();
    }

    public Node PopFromEnd ()
    {
            Node temp = new Node (list_ [list_.Count - 1]);
            list_.RemoveAt (list_.Count - 1);
            return temp;
    }

    public Node PeekFromEnd ()
    {
            return list_ [list_.Count - 1];
    }

    public void Add (ref Node toAdd)
    {
            Debug.Log (toAdd);
            list_.Add (toAdd);
            list_.Sort (sorter_);
    }
}

When I do now

Node temp = new Node(10,20); //custom constructor
PriorityListOfNodes l = new PriorityListOfNodes();
l.add(temp); 

I get runtime exception :

Object reference not set to an instance of an object

I have also tried without ref but with the same result. What am I doing wrong here ?

like image 605
Patryk Avatar asked Dec 20 '12 23:12

Patryk


1 Answers

You never actually instantiate a List<Node>.

 public PriorityListOfNodes ()
    {
            sorter_ = new NodeSorter_fValueDescending ();
            list_ = new List<Node>();
    }
like image 188
Pete Avatar answered Nov 15 '22 08:11

Pete