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.