When would SqlCommand.ExecuteReader() return null?

It’s a false positive. Reflecting on SqlDataReader.ExecuteReader, I can see that the only way the reader is returned as null is if the internal RunExecuteReader method is passed ‘false’ for returnStream, which it isn’t. In the depths of SqlDataReader, a reader constructor is always called at some point, so I’m pretty sure this is not … Read more

Initializing list property without “new List” causes NullReferenceException

It’s not broken syntax, it’s you who uses an object initializer on a property that’s simply not instantiated. What you wrote can be expanded to var parent = new Parent(); parent.Child.Strings = new List<string> { “hello”, “world” }; Which throws the NullReferenceException: you’re trying to assign the property Strings contained by the property Child while … Read more

Checking if an object is null in C#

It’s not data that is null, but dataList. You need to create one with public List<Object> dataList = new List<Object>(); Even better: since it’s a field, make it private. And if there’s nothing preventing you, make it also readonly. Just good practice. Aside The correct way to check for nullity is if(data != null). This … Read more

What does “Object reference not set to an instance of an object” mean? [duplicate]

Variables in .NET are either reference types or value types. Value types are primitives such as integers and booleans or structures (and can be identified because they inherit from System.ValueType). Boolean variables, when declared, have a default value: bool mybool; //mybool == false Reference types, when declared, do not have a default value: class ExampleClass … Read more