2

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; }
Artiom
  • 7,694
  • 3
  • 38
  • 45
user3663854
  • 463
  • 2
  • 8
  • 21
  • If you reset `myObj` to null you just let the reference *reference* a new instance, in this special case the `null`-instance. Of course such an "instance" is *not* initialized and thus has no properties set. I don´t understand what you mean and in particular what you expect to happen when you change it to `null`. – MakePeaceGreatAgain Sep 30 '16 at 05:41
  • @HimBromBeere: They are saying that they have a strange issue cropping up in production which, from looking at the code, _cannot_ happen, because at that point the reference is always initialized to an object instance (as far as they can tell). Changing the reference to point to `null` in the debugger of course reproduces the problem, but they can't see how it could happen in a running process. – Joey Sep 30 '16 at 05:43
  • 7
    pretty sure the exception occurs on the line before (`if (myModel...`) – jeroenh Sep 30 '16 at 05:43
  • Did you add a check to determine that `myObj` is actually null as you think it is? Perhaps the NullReferenceException is coming from the previous line? – JLRishe Sep 30 '16 at 05:43
  • 3
    May be exceptions actually happens at line `myModel.somePropertiesModel.someProperty.Count >= 1` ? – Artiom Sep 30 '16 at 05:44
  • 3
    I suppose not `myObj` is `null` (it surely **can´t** be), but `myModel` or any of its properties you´re trying to access the line above. Don´t trust the yellow line in your debugger for the source of an exception. – MakePeaceGreatAgain Sep 30 '16 at 05:45
  • @HimBromBeere I agree, myModel could only produce this exception – Artiom Sep 30 '16 at 05:45
  • In debugger, if I set myModel.somePropertiesModel.someProperty to null the exception log will show it happen there, but this is not the case, the exception shows it happen at the line I mention. System.NullReferenceException: Object reference not set to an instance of an object – user3663854 Sep 30 '16 at 05:46
  • provide exception message here please – Artiom Sep 30 '16 at 05:46
  • You should maybe - when the exception occurs - click `edit` on the message. Then you see the *exact* line of error. – MakePeaceGreatAgain Sep 30 '16 at 05:47
  • ERROR: 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 – user3663854 Sep 30 '16 at 05:49
  • 1
    At the beginning of the method, check if MyModel is not null. – Artiom Sep 30 '16 at 05:53
  • btw, the next code release is only 2 weeks after, I wish I can make new code deployment now. But until then, I cant try to figure this out without modifying the code and have it deployed again... – user3663854 Sep 30 '16 at 05:53
  • ok I think I will check myModel is null or not. Maybe the exception showing the wrong line number. – user3663854 Sep 30 '16 at 05:54
  • To give advice, so that in 2 weeks time, I made the correct code adjustment. I hope you understand my situation. For me, it is really clear that this shouldnt happen. But maybe my knowledge is limited, so this is why I ask... to be on the safe side. – user3663854 Sep 30 '16 at 05:57
  • 1
    Are you rsure `myObj.itsProperty= 1;` is line 197 of the source file when it was used to build the version running in the productive environment or is thjat just a guess? – helb Sep 30 '16 at 06:10
  • How about you add the calling code as well and the object json data as well? – Janne Matikainen Sep 30 '16 at 06:31
  • 1
    I've very same doubt at @helb. You might want to checkout the very same version from source control with which the production release was sent. Even if a line has changed in the source code since prod release then things are going to be messy to debug. – RBT Sep 30 '16 at 06:32
  • I accepted the answer below. Indeed that, the stacktrace is misleading me. The discussion below/above has made me focus on different line of error, and the accepted answer is the one solved the issue. Thanks all for helping to make me shift my focus elsewhere. Maybe when they merge the code, it became different. Though I checked at least 2 times before I post the question, I suppose the difference is there somewhere, but I cant find it. But it is there. As of now, no issue in production. Thanks! – user3663854 Oct 17 '16 at 02:03

1 Answers1

2

Most probably myModel is null but not myObj. At the beginning of the method add

if(myModel?.somePropertiesModel?.someProperty==null)
  throw new ArgumentNullException("myModel");

it's equivalent to

if(myModel==null || myModel.somePropertiesModel==null || myModel.somePropertiesModel.someProperty==null)
  throw new ArgumentNullException("myModel");

Or split it to 3 checks and throw exception with concrete information what object is null

if (myModel == null)
    throw new ArgumentNullException("myModel");
if (myModel.somePropertiesModel == null)
    throw new ArgumentNullException("myModel.somePropertiesModel");
if (myModel.somePropertiesModel.someProperty == null)
    throw new ArgumentNullException("myModel.somePropertiesModel.someProperty");

Also, there's possibility that getter of itsProperty produces this exception in case it does some work inside

Artiom
  • 7,694
  • 3
  • 38
  • 45
  • How come `myModel` object be null? `ToMyLog` is an extension method on `MyModel` class . `myModel` must be a non-null instance for `ToMyLog` method to be even get invoked? Program will throw exception before entering into the method `ToMyLog`. – RBT Sep 30 '16 at 06:13
  • @RBT no, extensions work fine even if an object is null. Extensions are converted to simple static method call. So, it's not the case. – Artiom Sep 30 '16 at 06:15
  • You were right. That was eureka moment for me with regards to how extension methods calling works. Too much of a compiler trick I would say. Thank you for the enlightenment. I wanted to give you +2 (1 for this new piece of information and 1 for potentially correct solution) but I can't. – RBT Sep 30 '16 at 06:42
  • it surprised me too at first, but when you understand how they actually work everything becomes pretty clear. – Artiom Sep 30 '16 at 06:45