-2

I was going to write up a question on this very peculiar issue I was having, but while writing the question found my own answer. So I figured I would go ahead and post this for the benefit of others and give my answer below.

I have the following foreach loops in my view:

foreach(CustomObject rt in Model.CustomObjectList1) {
   ...
}
foreach(CustomObject rt in Model.CustomObjectList2) {
   ...
}
foreach(CustomObject rt in Model.CustomObjectList3) {
   ...
}
foreach(CustomObject rt in Model.CustomObjectList4) {
   ...
}
foreach(CustomObject rt in Model.CustomObjectList5) {
   ...
}
foreach(CustomObject rt in Model.CustomObjectList6) {
   ...
}

Model.CustomObjectList1 through Model.CustomObjectList6 are all defined as List<CustomObject>, as I have to place these custom objects into different buckets and iterate through each to display on the view.

Before today, this code has worked fine for a long time. Today, I made an edit to the view to add the following hidden input field outside the loops at the end of the page:

<input type="hidden" id="otherObjectId" name="otherObjectId" value="@Model.OtherObject.ID" />

After making that change and trying to test my change, I now get a NullReferenceException error after iterating through Model.CustomObjectList6.

Debugging, I can see it looping through all the objects that populate Model.CustomObjectList6, and then after reaching the end of what should be the final iteration the code execution goes back to the top of the foreach loop and throws the NullReferenceException, with the debugger highlighting the "in" in the loop declaration.

I only get this problem on Model.CustomObjectList6. List 1-5 iterate just fine with no errors. I've also tried loading different datasets into this view as different users get different results by design, and the issue persists.

For example, if there are 10 objects in the list, I step through the code for each of the 10 objects and then get the error as if it is trying to execute an 11th iteration that doesn't exist.

To top it off, even if I change the iteration to a standard loop like this (since I know Model.CustomObjectList6 contains exactly 10, non-null items)...

for(int x=0; x<10; x++) {
   CustomObject rt = Model.CustomObjectList6[x];
   ...
}

...I still get the NullReferenceExceptionError and the debugger highlights x<10 as the point of the NullReferenceException error. I can watch it loop through the 10 proper iterations, go back to the top (where it highlights x++), next step it tries to evaluate x<10, and then throws the error.

Granicus
  • 624
  • 6
  • 16

1 Answers1

0

The answer is actually that new hidden input field. I am not sure why it manifested the way it did with the NullReferenceError on the for/each loops, but after reverting the page to the pre-edit version it started working again.

I added the input field again, and this time when running the page the NullReferenceException occurred on the new input field, which allowed me to identify that the OtherObject was null.

Populating OtherObject resolved the issue. Why the NullReferenceException manifested on the loop and not the new input field, I don't know, especially since there were 5 other hidden fields being populated by values from the model between the end of the loop and the new field.

Granicus
  • 624
  • 6
  • 16
  • 2
    how did you answer your own question in under a minute? – Josh Stevens Apr 10 '17 at 23:41
  • 2
    @JoshStevens, Win, there is an option to self-answer the question when posting. No trickery required. – BradleyDotNET Apr 10 '17 at 23:45
  • 4
    It's ok to post a question & self-answer when you already know the answer, if it's something new and useful http://stackoverflow.com/help/self-answer – hatchet - done with SOverflow Apr 10 '17 at 23:45
  • 1
    why post when you know the answer.. i know share the knowledge and all that but the question above makes completely no sense to do it! lol @BradleyDotNET – Josh Stevens Apr 10 '17 at 23:49
  • 1
    @JoshStevens Not commenting on whether or not this was a good idea; just that that's how you ask and answer a question at the same time – BradleyDotNET Apr 10 '17 at 23:50
  • haha sorry i was actually taking the p*** on my first comment! thanks for the clarification though dude @BradleyDotNET – Josh Stevens Apr 10 '17 at 23:51
  • Why do it? I had already typed up the entire question, and was trying some things out to add more detail when I came across the answer. I'd already gone to the effort, and it was such a weird scenario that I thought it would be useful if other people came across the same issue. – Granicus Apr 11 '17 at 13:18