Home > Back-end >  Is it possible to automate printing the variable which is null in a NullReferenceException without V
Is it possible to automate printing the variable which is null in a NullReferenceException without V

Time:09-06

Suppose I have a class with a lot of members and nested members and do this call and one of the variables is null (other than the last on each side of the equation):

Obj.Field.NextField.NextNextField = NextObj.OtherField.NextOtherField.NextNextOtherField;

I will get a NullReferenceException saying that an object is null in this line. In this case, there are 6 different cases where the exception could originate from.

I had this situation quite often by now and always do the following to figure out which member actually is null.

public static void PrintNullOrigin(params object[] _args)
    {
        for (int i = 0; i < _args.Length; i  )
        {
            if (_args[i] == null)
            {
                print(_args[i]   " is null!");
            }
        }
    }

But in this case, you still had to do it like this:

PrintNullOrigin(Obj, NextObj, Obj?.Field, NextObj?.OtherField, ...);

Is there a way that any time a NullReferenceException is thrown, a logger will do this automatically?

I tried getting the members in the line where the NullReferenceException originated from by the Exception object, but I can't find anything that makes it easy.

EDIT:

I have to highlight here, that I want to know if it is possible for this to be made outside the Visual Studios debugging mode.

In Unity the debugging mode of Visual Studio should be rarely used due to crashes that could occur and performance loss when running.

CodePudding user response:

There is no way to print which one of fields is null when you access it. You still have to do some check before accessing the field. This check can be in many ways:

  1. You can some function like CheckForNull(NextObj, "NextObj.OtherField.NextOtherField.NextNextOtherField") and then by splitting the second parameter and using Reflection - you can investigate which of fields in this path is null
  2. This way is a bit more complex: the second parameter can have type like Expression<Func<NextObj, T>> and value as obj => NextObj.OtherField.NextOtherField.NextNextOtherField. So you'll have expression of this mapping and you can find which of fields is null by examining this expression tree.

But overall if you have something like this: "NextObj.OtherField.NextOtherField.NextNextOtherField" This is bad sign - because it is poorly architected application when you have to do this.

CodePudding user response:

you can do something like this:

var propertiesTree = "NextObj.OtherField.NextOtherField.NextNextOtherField".Split('.');
object obj = NextObj;
for(var index = 1; index < propertiesTree.Length; index  )
{
    var property = obj.GetType().GetProperty(propertiesTree[index]);
    var value = property.GetValue(obj);
    if (value == null) { throw new Exception($"{propertiesTree[index]} is null"); }
    obj = value;
}

// if we are here - then all fields are not null
  • Related