我想用自定义类包装List类.至于现在我有这样的事情;
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_); } }
我什么时候做
Node temp = new Node(10,20); //custom constructor PriorityListOfNodes l = new PriorityListOfNodes(); l.add(temp);
我得到运行时异常:
Object reference not set to an instance of an object
我也试过没有参考,但结果相同.我在这做错了什么?
解决方法
您永远不会实际实例化List< Node>.
public PriorityListOfNodes () { sorter_ = new NodeSorter_fValueDescending (); list_ = new List<Node>(); }