Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对象引用未设置为对象链表示例的实例_C# - Fatal编程技术网

C# 对象引用未设置为对象链表示例的实例

C# 对象引用未设置为对象链表示例的实例,c#,C#,我看到以下错误: 对象引用未设置为对象的实例 在调用方法之前,请检查以确定对象是否为null 我是C#新手,我制作了一个排序链表程序。 下面是错误出现的代码 public void Insert(double data) { Link newLink = new Link(data); Link current = first; Link previous = null; if (first == null)

我看到以下错误:

对象引用未设置为对象的实例

在调用方法之前,请检查以确定对象是否为null

我是C#新手,我制作了一个排序链表程序。 下面是错误出现的代码

    public void Insert(double data)
    {
        Link newLink = new Link(data);
        Link current = first;
        Link previous = null;

        if (first == null)
        {
            first = newLink;
        }
        
        else
        {
            while (data > current.DData && current != null)
            {
                previous = current;
                current = current.Next;
            }

            previous.Next = newLink;
            newLink.Next = current;
        }
    }
它说当前引用为null
,而(data>current.DData&¤t!=null)
,但我将其赋值为:
current=first

剩下的是完整的程序代码

class Link
{
    double dData;
    Link next=null;

    public Link Next
    {
        get { return next; }
        set { next = value; }
    }

    public double DData
    {
        get { return dData; }
        set { dData = value; }
    }

    public Link(double dData)
    {
        this.dData = dData;
    }


    public void DisplayLink()
    { 
    Console.WriteLine("Link : "+ dData);
    }

}

class SortedList
{
    Link first;

    public SortedList()
    { 
        first = null;
    }

    public bool IsEmpty()
    {
        return (this.first == null);
    }

    public void Insert(double data)
    {
        Link newLink = new Link(data);
        Link current = first;
        Link previous = null;

        if (first == null)
        {
            first = newLink;
        }
        
        else
        {
            while (data > current.DData && current != null)
            {
                previous = current;
                current = current.Next;
            }

            previous.Next = newLink;
            newLink.Next = current;
        }
    }

    public Link Remove()
    {
        Link temp = first;
        first = first.Next;
        return temp;
    }

    public void DisplayList()
    {
        Link current;
        current = first;

        Console.WriteLine("Display the List!");
        
        while (current != null)
        {
            current.DisplayLink();
            current = current.Next;
        }
    }
}

class SortedListApp
{
    public void TestSortedList()
    {
        SortedList newList = new SortedList();
        newList.Insert(20);
        newList.Insert(22);
        newList.Insert(100);
        newList.Insert(1000);
        newList.Insert(15);
        newList.Insert(11);

        newList.DisplayList();
        newList.Remove();
        newList.DisplayList();

    }
}

同意你所做的

current = first; 
但是第一个类的开头是空的

class SortedList
{
    Link first;

请先为
分配一些内容
,否则它将为空

您可能假设while循环在第一次迭代时中断,而不是因为while循环中的分配才最终中断它

最终,根据您的代码,current为NULL,您甚至可以对其进行测试-将其更改为此,应该可以:

while (current != null && data > current.DData)
{
    previous = current;
    current = current.Next;
}

您如何知道
first
不是
null
?(在我看来,在一个空列表中,它应该是。)此外,还有像
data>current.DData&¤t!=null
从左到右短路。这意味着
data>current.DDAta
current!=空
。结果是,如果
current
为null,则组合表达式将崩溃,尽管
current!=null
check,因为后者发生得太晚了。while(current!=null&&data>current.DData)谢谢,现在我意识到问题出在哪里了,现在它对列表进行了很好的排序!别担心,请你把答案标为正确(如果确实正确的话)。干杯,罗布