0

I am using Visual Studio 2017 with the ASP.NET Core 2.1 Framework. When I try and loop through a static object I have in my controller, the iterator seems to be null.

The static object I am calling definitely has a collection seen below: collection

However When I try and see what the details are of the iterator I get a null reference exception even through the iterator should have a value:

enter image description here

What would be the cause of this null reference?

EDIT: Thanks to the answer I was able to debug what the issue was.

In the code where I am setting the bool wasFound I am doing a where query against a list of objects where the name of the incoming connection is in the list. In this case, the connection name was null and so it would fail.

Obviously the null reference error was a bit vague as to what was causing the error.

JamesS
  • 1,643
  • 1
  • 7
  • 19
  • 1
    When you are halting at that breakpoint, can you double check the source that there are really no null values inside? – poke Apr 01 '20 at 10:02
  • @poke When halting at the breakpoint, there are definitely values in the collection of _uploadedFileModel.routemaps – JamesS Apr 01 '20 at 10:07
  • 2
    Could be that some property of `i` that the debugger is trying to show when you inspect the variable is throwing a null reference. I imagine that if you expand any of the collection items the debugger is showing in your first image, you'll get the same error. – InBetween Apr 01 '20 at 10:10

1 Answers1

1

The problem is not with i itself, most probably the incoming instance is null, you can use ?. operator in this case:

i?.incoming?.connection
Salah Akbari
  • 36,933
  • 10
  • 58
  • 91
  • See the second image. `i` already throws in the immediate window. – HimBromBeere Apr 01 '20 at 10:05
  • I'm not sure, the problem definitely seems to. be with `I`. I cannot hover over `I` even with a breakpoint on the first `{` and when going into the immediate window, `I` still says null – JamesS Apr 01 '20 at 10:08
  • @JamesS What happens after you use my solution? – Salah Akbari Apr 01 '20 at 10:13
  • Yeah. I checked against both `I`, `incoming` and `collection` and it appears that doing so solved my issue. – JamesS Apr 01 '20 at 10:15
  • 1
    This “solution“ will just skip the items altogether. But if there is an unexpected null value inside the collection, which makes `i` null, then that should be properly solved instead of ignored. – poke Apr 01 '20 at 10:15
  • To me it just seems a bit odd. Elsewhere in the application I loop through the exact collection and everything is fine. However here it is not. Would this exception be thrown if ANY of the items in the collection are null? So for example index [0] is fine but index [12] has a null variable? – JamesS Apr 01 '20 at 11:05
  • @JamesS You can use `?.` operator after all of the chain instances and indexes, please read about `?.` in more details and examples in the link I've included. – Salah Akbari Apr 01 '20 at 11:10
  • @JamesS Yes, if there is just a single item in the collection that is null, then for that item’s iteration, `i` will be _that_ null. – poke Apr 01 '20 at 11:30
  • Thanks. I'll put an edit on the question as to what the problem was – JamesS Apr 01 '20 at 11:42