-3

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.

ElGavilan
  • 6,610
  • 16
  • 27
  • 36
  • Please take a moment to read through the [edit help](http://stackoverflow.com/editing-help). Your post has code that is very hard to read. Formatting is different on Stack Overflow than other sites. – gunr2171 May 30 '14 at 20:30
  • 1
    You have a NullReferenceException. The cause for this is always the same. You're trying to use an object that hasn't been initialized (it's null). So figure out what it is, then work around it. I suggest using your IDE's debugging tools to help you figure out what is null. Also, make sure you use the code formatting when you post a question so that the question is easily legible (go ahead and edit your question to fix it). Welcome to Stack Overflow! – mason May 30 '14 at 20:31
  • TheList.Devices is never initialized. – eddie_cat May 30 '14 at 20:31

1 Answers1

1

Devices from TheList as not been initialized, and/or uniqueitems as not been initialized, use new to initialize an object or an array, ie:

    TheList.Devices = new long[N];
quantdev
  • 23,517
  • 5
  • 55
  • 88
  • Thanks karim - that helped! Sorry for my poor code display, and thanks for the suggestions. I did view the other post that is being recommended, but I didn't see how it applied to this case, which I see it does after your assistance. Thanks. – user3587069 May 30 '14 at 20:55