调试:'Object reference note set to an instance of an object.'
今天调试代码遇到一个奇怪的问题,每次调试到 var files = new List<string>()这一行代码,总是报错:System.NullReferenceException: 'Object reference not set to an instance of an object.'
但是怎么看都没觉得代码有问题,甚至把这行代码多复制几行,也只有这一行代码会出错。
经过研究发现,这个变量List<string> files在后面的匿名函数中被使用到,而我在调试代码的时候,为了方便,直接用鼠标拖动调试进度,导致跳过了一些花括号。这可能会导致匿名函数的作用域会有问题。
public static void Star()
{
int i = 1;
i++;
{
var ls = new List<string>();
Func<List<string>, int> f = (ls2) => { return ls2.Count; };
//ls.ForEach((f) =>
//{
// int count = ls.Count;
//});
}
}
一段示例代码,在i++这打上断点。然后等程序运行到i++,继续正常运行,则代码不会有问题,如果代码运行到i++,用鼠标往后拖动进度,跳过花括号,则会报错:'Object reference note set to an instance of an object.'
不知道为什么,用js的思想理解,这很可能是创建匿名函数的时候,就会创建一个闭包,闭包就是保存了函数的运行环境:变量、作用域等。在C#中,花括号也是作用域,我们拖动进度跳过花括号,可能会导致闭包生成的作用域与代码定义的不同,所以闭包内的变量ls无法从作用域中找到这个对象,所以就报错了。
调试:'Object reference note set to an instance of an object.'的更多相关文章
- ArcGIS AddIN异常之:object reference not set to an instance of an object
异常出现在 frmDownload frd = new frmDownload(); frd.ShowDialog(); 在ArcMap中能正常弹出窗体,点击按钮时显示此异常:object refer ...
- Azure Sphere–“Object reference not set to an instance of an object” 解决办法
在开发Azure Sphere应用时,如果出现项目无法编译,出现“Object reference not set to an instance of an object”时,必须从下面两个方面进行检 ...
- Visual Studio 2015打开ASP.NET MVC的View提示"Object reference not set to an instance of an object"错误的解决方案
使用Visual Studio 2013打开没有问题,但Visual Studio 2015打开cshtml就会提示"Object reference not set to an insta ...
- [Bug]Object reference not set to an instance of an object.
引言 今天在客户这儿,由一个问题导致,需求的变化,不得不修改代码,在记录日志中出现该问题. 原因 通过id查找相关信息,没有判断是否为null,集合是否有数据. Object reference no ...
- C# Object reference not set to an instance of an object.
一.问题 Object reference not set to an instance of an object. (你调用的对象是空的) 二.解决问题 在使用 c# 的查询时,先筛选后在关联其他表 ...
- 错误“Object reference not set to an instance of an object”的解决方法
在进行unity游戏制作的C#代码编写时,会遇到“NullReferenceException: Object reference not set to an instance of an objec ...
- 【Azure API 管理】使用APIM进行XML内容读取时遇见的诡异错误 Expression evaluation failed. Object reference not set to an instance of an object.
问题描述 使用APIM,在 Inbound 中对请求的Body内容进行解析.客户端请求所传递的Request Body为XML格式,需要从Request Body中解析出多个(Element)节点值, ...
- Spine用于Timeline(NullReferenceException: Object reference not set to an instance of an object pine.Unity.Editor.AnimationReferenceAssetEditor.OnInspectorGUI ())
报错信息:Spine.Unity.Editor.AnimationReferenceAssetEditor.OnInspectorGUI () (at Assets/Extention/Spine/E ...
- WebAdaptor Object reference not set to an instance of an object.
C:\inetpub\wwwroot\arcgis目录下webAdaptor.config文件内容被清空,从别的地方拷贝一份即可. <?xml version="1.0" e ...
随机推荐
- 洛谷3317 SDOI2014重建(高斯消元+期望)
qwq 一开始想了个错的做法. 哎 直接开始说比较正确的做法吧. 首先我们考虑题目的\(ans\)该怎么去求 我们令\(x\)表示原图中的某一条边 \[ans = \sum \prod_{x\in t ...
- 2020.3.28-ICPC训练联盟周赛,选用试题:UCF Local Programming Contest 2016
A.Majestic 10 签到题. #include<iostream> #include<cstdio> #include<cstring> #include& ...
- 【UE4 C++】定时器 Timer 与事件绑定
概念 定时执行操作,可执行一次,或循环执行直到手动终止 定时器在全局定时器管理器(FTimerManager 类型)中管理.全局定时器管理器存在于 游戏实例 对象上以及每个 场景 中 定时器需要绑定委 ...
- Prometheus监控Canal
Prometheus监控Canal 一.背景 二.实现步骤 1.修改prometheus.yml配置文件 2.启动prometheus 3.查看prometheus是否成功接入canal 4.cana ...
- 21.7.24 test
\(NOIP\) 模拟赛 考差了. T1签到题.注意存在字符串长度为0,不能直接模.\(100\rightarrow0\) 代码: #include<bits/stdc++.h> usin ...
- 单源最短路径算法:迪杰斯特拉 (Dijkstra) 算法(一)
一.算法介绍 迪杰斯特拉算法(英语:Dijkstra's algorithm)由荷兰计算机科学家艾兹赫尔·迪杰斯特拉在1956年提出.迪杰斯特拉算法使用了广度优先搜索解决赋权有向图的单源最短路径问题. ...
- hdu 1160 FatMouse's Speed(最长不下降子序列+输出路径)
题意: FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to ...
- hdu 5102 The K-th Distance (队列+生成法,,)
题意: N个点的一棵树.定义点u和点v的距离等于它们之间的路径(唯一的)的长度.这样我们可以得到n*(n-1)/2个距离. 将它们从小到大排序,问前K个数的和是多少. 思路: 将边长为1的树枝都入队列 ...
- uni-app使用wx-canvas实现微信小程序上显示地图map和坐标geo
源码 <template> <view class="echart-box"> <canvas class="ec-canvas" ...
- Python常用的数据文件存储的4种格式(txt/json/csv/excel)及操作Excel相关的第三方库(xlrd/xlwt/pandas/openpyxl)(2021最新版)
序言:保存数据的方式各种各样,最简单的方式是直接保存为文本文件,如TXT.JSON.CSV等,除此之外Excel也是现在比较流行的存储格式,通过这篇文章你也将掌握通过一些第三方库(xlrd/xlwt/ ...