【问题标题】:Question about NullExceptionError in C#C#中关于NullExceptionError的问题
【发布时间】:2011-07-23 10:17:32
【问题描述】:

查看下面的函数测试代码:

 List<Square> tempArr = new List<Square>();
 Coin tempCoin = new Coin(eCoinType.White);
 Square tempMove = new Square(1, eColumn.A, tempCoin);
 tempArr.Add(tempMove);
 m_OtheloGame.isLegalMove(tempMove, ref tempArr);

编译器在最后一行用 NullExceptionError 绘制“tempArr”。

我不明白为什么,因为我已经在第一行分配了 tempArr,所以它不能为空。

【问题讨论】:

  • 你能解释一下“编译器绘制 tempArr”是什么意思吗?
  • 是的,我的意思是它带有红色下划线
  • 当你尝试编译它时输出是什么?
  • 您可以发布异常的详细信息吗?调用堆栈中的内容可能比我们在问题中看到的要多。
  • 真的需要通过引用传递tempArr吗?

标签: c# nullreferenceexception compiler-errors


【解决方案1】:

isLegalMove() 方法是做什么的,因为 tempArr 作为 ref 传入,所以它可能被更改为引用 null

【讨论】:

    【解决方案2】:

    isLegalMove 方法中查找问题。 此外,转到 Debug/Exceptions 并在两列上启用公共语言运行时异常:Thrown 和 User-unhandled。然后再次运行,应该会在isLegalMove 方法内部通知您实际错误发生的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 2011-04-17
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      相关资源
      最近更新 更多