-1

I have a system which manages tasks, and after leaving it running overnight came back to a NullReferenceException; "Object reference not set to an isntance of an object."

I've read half a hundred other NullReferenceException SO posts, and none seem to share my issue. The system ran perfectly fine for 8 hours before crashing, while debugging I can see the locals of the line the exception was raised, and nothing is null. Here is some code

if (ackList.Count > 0)
{
    for (int i = 0; i < ackList.Count; i++)
    {
        if ((t.taskID == ackList[i].taskID) && (ackList[i].associatedFile != null)) //exception raised on this line
        {
            //process task as complete
            //Locals as below:
            //t.taskID = 68941
            //i = 30840
            //ackList.Count = 30841
            //ackList[i].taskID = 68941
            //ackList[i].associatedFile = {TaskACKAssociatedFile[1]}
        }
    }
}

I inherited this code from a previous dev, and the loop was then a foreach which fell apart when ackList increased during the running of the loop; ackList stores all acknowledgement messages and is populated in a different thread.

The exception is raised off the if statement, where at least in this case there are no null values.

EDIT: I would argue that this is not an exact duplicate of What is a NullReferenceException, and how do I fix it? . All my objects are initiated, and none of the locals used are null. That question is asking what a NRE is, of which I am frightfully aware, and none of the answers, even that incredibly thorough top answer, helps me whatsoever.

alzinos
  • 71
  • 10
  • You can use debug and see ackList[i].taskID or ackList[i].associatedFile value. And set for (int i = 0; i < ackList.Count - 1; i++). – Tomato32 May 24 '17 at 08:51
  • You say that _nothing is null_. However, given the exception obviously something is null. With the code you have provided then either `t` or `ackList[i]` is null. You can check for this in the loop. However, if your code has a race condition (you talk about multiple threads) then you might still get the exception. – Martin Liversage May 24 '17 at 08:51
  • this sounds like a very dynamic procedure which could be difficult to debug by hand, especially if you ran it for 8 hours. Do you have some sort of logging? or any possibility to log the items? – Mong Zhu May 24 '17 at 08:52
  • what is the type of `t` ? and of what type are the elements in `ackList` ? – Mong Zhu May 24 '17 at 08:53
  • @Tomato32 As in the locals I commented in above, ackList[i] has the values specified. Neither are null. Would that not cut off the last value? Say I have a list 100 items long, from index 0 to 99; would (int i = 0; i < Count - 1; i++) not go from index 0, to less than index (100 - 1), so only to index 98? – alzinos May 24 '17 at 09:07
  • @MartinLiversage I accept that the exception is coming from somewhere. But none of the variables used in the if statement appear to me to be null. I'm happy to shove in a bit of validation, but given the time it ran for successfully I don't want to spend all day waiting on it to break. There are no race conditions as far as I'm aware. Again, it ran perfectly for 8 hours with no issues. The other thread in this case is purely to capture incoming messages and populate the ackList. This code is happy to wait until something is added to the list. – alzinos May 24 '17 at 09:08
  • @MongZhu That is my main concern, I can't leave it running all day and hope to replicate the issue. I'll set it going overnight to see if its replicable, but thats a long work day away. No logging of individual variables currently, but before running it again I'll set some up. t is of type "Task", which contains a couple of location fields, some time fields. t here is fully populated. The items in ackList are of type "TaskACK", and all fields used are not-null. – alzinos May 24 '17 at 09:11

1 Answers1

0

Either t is null, or your ackList[i] is null.

That ackList object. This is only an assumption, but is there any chance it could be being changed elsewhere on another thread?

If so, try using lock to avoid concurrency issues like this. Have a look here

  • As far as the locals appear to me, neither _t_, nor _ackList[i]_ is null. _t_ is a task object, with all values as expected; some position data, some time data, no null values within it. And _ackList[i]_ appears the same, _taskID_ is populated, as are all the other fields. _ackList_ should be increasing in size with time, but currently is never edited, or shrunk. Only extra values added to the end. – alzinos May 24 '17 at 09:01