得到一个nullreference异常错误c# asp.net mvc

本文关键字:错误 异常 asp mvc net nullreference 一个 | 更新日期: 2023-09-27 18:06:11

我试图通过检查有效id的最后一次活动来构建一个数组。我就在终点线,当我找到一个活动设备时,我得到一个空异常错误。"thestream"变量包含JSON,并包含如下内容:

{"域":"d50fb707f9317","东西":"通讯"、"事":"000780000001","m2m_raw":"{'"命令'":100年,'"电流'":25日,'"cap-max '":1200年,'"cap-remain '":339}","m2m_nestedkey":["系统特征"、"诊断信息"、"诊断Information2"、"系统设置"、"阀门设置","时钟":0,"属性":{"电流:钟":"1401480786291382"、"重量":"150","cap-max":"1200","名字":"valve12"、"电流":"25","命令":"100","你好:钟":"1401201915442520","名字:钟":"1401276113497347","cap-remain:钟":"1401480786291382","你好":"有","cap-max:钟":"1401480786291382"、"命令:钟":"1401480786291382","cap-remain":"339","体重:钟":"1401275692557475"}}

我的模型中有这样的声明:

 public class ListofDevices
    {
        public long[] Devices { get; set; }
    }

在控制器中这样声明:

var TheList = new ListofDevices();

activeCount被声明并设置为零:

byte activeCount = 0; //(the highest number in here is 36)

下面所有的代码都在控制器中。

JObject CheckIt = JObject.Parse(thestream);  // parse the Json as best as we can, given the  json we are getting back
string ClockNumString = (string)(((Newtonsoft.Json.Linq.JValue)((((((Newtonsoft.Json.Linq.JContainer)(CheckIt)).Last).First).First).First)).Value);
long ClockNum = Convert.ToInt64(ClockNumString); // convert the string into a number
ClockNum = ClockNum / 1000000; // convert to seconds
TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);  // get the current epoch time
long secondsSinceEpoch = (long)t.TotalSeconds;  // convert to seconds for comparison
if ((secondsSinceEpoch - ClockNum) < 60000) {
     TheList.Devices[activeCount] = Convert.ToInt64(uniqueitems[i]);  // ERROR IS HERE
     activeCount++;
}

我疯狂的Newtonsoft.Json.Linq命令是因为返回的JSON不能以任何其他方式处理。我进入ClockNumString的数据是正确的。但是当我试图将项目存储到活动数组中时,我得到了nullreferenceexception错误,它传递了if语句。这似乎是一个类型错误(?),但我已经搜索了stackoverflow的帮助,并一直在这个工作一整天,但没有运气。如有任何帮助,不胜感激。

得到一个nullreference异常错误c# asp.net mvc

TheList中的Devices未初始化,和/或uniqueitems未初始化,使用new初始化对象或数组,即:

    TheList.Devices = new long[N];