1

I am trying to test some code and now visual studio is throwing a null reference exception on the following line:

List<int> liveIds = new List<int>();

I am starting to think that visual studio has old code that it is looking at because no matter how I have tried to declare this line it continues to throw a null reference exception on this line.

Anyone know something that I might be missing?

UPDATE: ok so I changed out the variable and now I can't get the same error to happen on the previous line. Now it is happening on this line.

enter image description here

10
  • There is no chance for NullReferenceException in the above line, It looks fine Commented Jan 19, 2017 at 3:43
  • This line is valid. Can you share other code lines? Commented Jan 19, 2017 at 3:44
  • This is the problem. I honestly can not see why it is giving me this error. That is why I feel like VS must be loading a cached assembly possibly. Where I had other code that might have had an error. Commented Jan 19, 2017 at 3:44
  • 1
    you might do it already but did you try to restart VS? you know sometimes VS just act weird. Commented Jan 19, 2017 at 3:47
  • 1
    The exception is being thrown in a dependency, not in your code per se. It is likely a config issue with that dependency. Commented Jan 19, 2017 at 4:34

2 Answers 2

2

Ok so after some testing, things worked once I refactored the code and extracted the following code to a new method:

        // remove hospitals that are not currently assigned to someone
        hospitalsToCheck.RemoveAll(
            h =>
            {
                return
                    !currentAssignments.Exists(
                        a => a.AssignmentGroup.AssignedUnitIds.Intersect(h.Units.Select(u => u.UnitId)).Any());
            });

It seems that when I had code that was manipulating the list in the same method that it was defined, that is when I was getting the null reference exception.

Sign up to request clarification or add additional context in comments.

1 Comment

glad to know that you find the real reason, if so, would you please mark it as the answer? So it could help other members who meet the same issue as yours. Have a nice day:)
1

Maybe the debugger itself is producing the exception.

I got a very similar situation, where i'm assigning a value from a function and the exception occurs right after executing the function (the Locals window indicates that the function does return a value):

var xmlElement = Serialize(data);

From Disassembly it seems that the exception happens immediately after the assignment, but before the next line of code. I'm guessing this is where some debugger code gets executed:

Screenshot of disassembly

Maybe debugger did not expect for that line of code to be executed, because I altered the execution path with "Set Next Statement" command (moved from the "else" block into this one). Same thing happens if I alter the code (remove the "if") while the debug is running, but the problem does not repeat if I rerun the encompassing procedure after the edit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.