未处理的异常。 System.NullReferenceException: 对象引用未设置到对象的实例
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object
我正在尝试打印 Stack 的内容。
尝试时出现以下错误。
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
这发生在我代码的 foreach 行。我不确定为什么会这样,因为我认为我使用的是链接页面上给出的示例。例子是...
foreach( string number in numbers )
{
Console.WriteLine(number);
}
以下是我的代码。除了抛出错误的这一部分外,一切似乎都可以正常工作。
foreach(var s in stack)
{
Console.WriteLine(s);
}
...这是我的代码。
using System;
namespace Exercise
{
class Program
{
static void Main()
{
var stack = new Stack();
stack.Push(1);
stack.Push(2);
stack.Push(3);
foreach(var s in stack)
{
Console.WriteLine(s);
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Exercise
{
internal class Stack : IEnumerable
{
private object _object;
private List<object> list = new List<object>();
private IEnumerator Enumerator;
public IEnumerator GetEnumerator() => Enumerator;
internal object Pop()
{
if (list.Count == 0)
throw new InvalidOperationException("Cannot use .Pop() if list count equals 0.");
_object = list.FirstOrDefault();
list.RemoveAt(0);
return _object;
}
internal void Push(object obj)
{
_object = obj;
if (_object == null)
throw new InvalidOperationException("Cannot use .Push() if object is null.");
list.Insert(0, _object);
}
internal void Clear()
{
if (list.Count == 0)
throw new InvalidOperationException("Cannot use .Clear() if list is empty.");
list.Clear();
}
}
}
我做错了什么,如何解决这个问题以打印堆栈的内容?
你的 GetEnumerator
方法 returns null 因为字段 Enumerator
从未被显式初始化,所以它得到默认值 null。
然后,foreach循环调用.GetEnumerator()
,接收到一个null并尝试访问null的.Current
属性,所以你得到一个NullReferenceExcpetion
.
要解决此问题,您可以使用以下实现:
public IEnumerator GetEnumerator()
{
while (list.Any())
yield return Pop();
}
我正在尝试打印 Stack 的内容。
尝试时出现以下错误。
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
这发生在我代码的 foreach 行。我不确定为什么会这样,因为我认为我使用的是链接页面上给出的示例。例子是...
foreach( string number in numbers )
{
Console.WriteLine(number);
}
以下是我的代码。除了抛出错误的这一部分外,一切似乎都可以正常工作。
foreach(var s in stack)
{
Console.WriteLine(s);
}
...这是我的代码。
using System;
namespace Exercise
{
class Program
{
static void Main()
{
var stack = new Stack();
stack.Push(1);
stack.Push(2);
stack.Push(3);
foreach(var s in stack)
{
Console.WriteLine(s);
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Exercise
{
internal class Stack : IEnumerable
{
private object _object;
private List<object> list = new List<object>();
private IEnumerator Enumerator;
public IEnumerator GetEnumerator() => Enumerator;
internal object Pop()
{
if (list.Count == 0)
throw new InvalidOperationException("Cannot use .Pop() if list count equals 0.");
_object = list.FirstOrDefault();
list.RemoveAt(0);
return _object;
}
internal void Push(object obj)
{
_object = obj;
if (_object == null)
throw new InvalidOperationException("Cannot use .Push() if object is null.");
list.Insert(0, _object);
}
internal void Clear()
{
if (list.Count == 0)
throw new InvalidOperationException("Cannot use .Clear() if list is empty.");
list.Clear();
}
}
}
我做错了什么,如何解决这个问题以打印堆栈的内容?
你的 GetEnumerator
方法 returns null 因为字段 Enumerator
从未被显式初始化,所以它得到默认值 null。
然后,foreach循环调用.GetEnumerator()
,接收到一个null并尝试访问null的.Current
属性,所以你得到一个NullReferenceExcpetion
.
要解决此问题,您可以使用以下实现:
public IEnumerator GetEnumerator()
{
while (list.Any())
yield return Pop();
}