I have a production code throwing exception at
myObj.itsProperty= 1;
System.NullReferenceException: Object reference not set to an instance of an object. at name.Extensions.Ads.Payload.ThisExtensions.ToMyLog(MyModel myModel, MyOwnModel myOwnModel) in D:\name\Extensions\Ads\Payload\ThisExtensions.cs:line 197
In local code the only way for me to force this to happen is to put the breakpoint there and manually change myObj to null.
But according to the code flow this should be already initialized...
I'm not entirely sure what is happening and how this happen. Is there a way to explain this, or perhaps strengthen the code to prevent this?
public static MyModel ToMyLog(this MyModel myModel, MyOwnModel myOwnModel)
{
DateTime currentTime = DateTime.Now;
MyModel myObj =
new MyModel
{
SomeID = 1
InsertedDate = currentTime,
UpdatedDate = currentTime
};
if (myModel.somePropertiesModel.someProperty.Count >= 1)
{
myObj.itsProperty = 1; //itsProperty is a byte type
}
MyModel class
public class MyModel
{
///<summary>
/// itsProperty
///</summary>
public byte itsProperty{ get; set; }