Below is the code and the problematic line.
When I src.EnergyServiceLevel over src.EnergyServiceLevel , it shows that it is zero. How can this be if I check the null value in the previous line?
I assumed that there might be topics that are creating the problem, so I am adding a lock, but that did not help.
public static ServiceLevelsGroup SafeClone(this ServiceLevelsGroup src) { ServiceLevelsGroup res = null; lock (_locker) { if (src != null) { res = new ServiceLevelsGroup(); if (src.EnergyServiceLevel != null) { res.EnergyServiceLevel = new ServiceLevelInfo { ServiceGrade = src.EnergyServiceLevel.ServiceGrade }; if (src.EnergyServiceLevel.Reason != null) res.EnergyServiceLevel.Reason = src.EnergyServiceLevel.Reason; } } } return res; }
An exception occurs on the line res.EnergyServiceLevel = ... in the code above.
Here is a screenshot of the exception that occurs in debug mode:

source share