When you have a statement like A.B.C = E.F;, and you receive an NullReferenceException 'Object reference not set to an instance of an object.' on that line of code, it basically means either A, or B or E are null.
To figure out what's wrong there, you can set a breakpoint on the same line, start debugging and then when the breakpoint hits, just check the value of A, B or E.
When NullReferenceException happens?
There are some common cases that you may encounter a NullReferenceException. Here is a summary based on NllReferenceEexception article in .NET documentations. I've put the link of the article in the next section in the answer, where you can find some useful example as well.
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null. A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios:
- You've forgotten to instantiate a reference type.
- You've forgotten to dimension an array before initializing it.
- You get a null return value from a method, and then call a method on the returned type.
- You're using an expression to retrieve a value and one of the intermediate values in the expression returns null.
- You're enumerating the elements of an array that contains reference types, and an element is null.
- A NullReferenceException exception is thrown by a method that is passed null.
Learn more
For more information and examples, take a look at the following links: