I'm trying to build an array of valid id's by checking the last time they were active. I am right at the finish line and I'm getting a null exception error when I find an active device. "thestream" variable has the JSON in it and contains this:
{"domain":"d50fb707f9317","stuff":"comm","thing":"000780000001","m2m_raw":"{\"command\":100,\"current-flow\":25,\"cap-max\":1200,\"cap-remain\":339}","m2m_nestedkey":["System Characteristics","Diagnostic Information","Diagnostic Information2","System Settings","Valve Settings"],"clock":0,"attributes":{"current-flow:clock":"1401480786291382","weight":"150","cap-max":"1200","myname":"valve12","current-flow":"25","command":"100","hello:clock":"1401201915442520","myname:clock":"1401276113497347","cap-remain:clock":"1401480786291382","hello":"there","cap-max:clock":"1401480786291382","command:clock":"1401480786291382","cap-remain":"339","weight:clock":"1401275692557475"}}
My Model has this declaration in it:
public class ListofDevices
{
public long[] Devices { get; set; }
}
It is declared in the controller like this:
var TheList = new ListofDevices();
activeCount is declared and set to zero:
byte activeCount = 0; //(the highest number in here is 36)
All of the code below is in the controller.
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++;
}
My crazy Newtonsoft.Json.Linq command is due to the fact that the JSON that is coming back can't be handled any other way. The data I'm getting into ClockNumString is correct. But I'm getting the NullRefenceException error when I try to store the item into the active array, that passes the if statement. It appears to be a type error(?), but I've scoured stackoverflow for assistance and have been working on this all day, but no luck. Any help greatly appreciated.