Variable used in LINQ method is throwing NullReferenceException on initialization?

While debugging code today, I ran into a NullReferenceException that should be impossible. It was on the assignment of an empty string "", not string.Empty (though they should be equivalient IIRC) to a new variable declaration:

Initial Exception Screenshot

Here's the code in a copypasta form:

string city = "";
string state = ""; // Throws null reference exception.

I initially thought that maybe, just maybe, somehow the same variable was declared elsewhere and wasn't throwing a compiler error, but after running a search through the code, that variable is created there, no where else.

From there, the second thought was that maybe the PDB files were out of sync, which was supported in comments from other SO users here. As such, a clean and re-build was in order, but alas did not resolve my issue.

I then cross compared my changes against the containing repo and noticed that my only change was to add a check to see if the variable state contained a known state abbreviation using a call to .Any:

List<string> validStates = StateList.Split(',').ToList();
if (validStates.Any(validState => validState.Equals(state, StringComparison.InvariantCultureIgnoreCase)))
    ... // Unchanged

I changed this to use .Contains instead and the issue disappeared:

List<string> validStates = StateList.Split(',').ToList();
if (validStates.Contains(state.ToUpperInvariant()))
    ... // Unchanged

Thinking this was a fluke, I did another clean & rebuild with the .Any call and it still failed, leading me to believe the issue is with the call to .Any. However, the call to this is almost 50 lines after the declaration of the variable state which makes no sense as to why it would cause the issue.

SO user @madreflection pointed out in the comments that this could be related to the capture by the lambda expression and could be leading to unexpected behavior, and requested that I try moving the declaration closer to the call to .Any. Due to the logic above, I couldn't move the creation of the statement, downward, but I did create a new variable, assigning it a value of "" and changed back to .Any using the test variable instead. The issue is for sure still occurring, but this time on the creation and initialization of my test variable:

Test Variable Screenshot with Exception

Here's the code from that screenshot in copypasta form:

string state = "";
...
string testState = "";
testState = state;
if (!string.IsNullOrWhiteSpace(testState)) {
    List<string validStates = StateList.Split(',').ToList();
    if (validStates.Any(validState => validState.Equals(testState, StringComparison.InvariantCultureIgnoreCase)))
        ... // Unchanged
}

As another update, I removed all the code between the declaration and the use of .Any and the error is still occurring. Also, I attempted to give the variable a valid starting value such as "AK" and it still blew up.


Why would assigning an empty string value to a variable throw a NullReferenceException when using the LINQ method .Any almost 50 lines later?

NOTE: There is no inner exception, and we have no other ideas as to what could cause this. If anyone has ideas I'll gladly update the post with more information.