Cannot find why I have a null reference exception

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:

Screenshot of exception while debugging

+4
source share
3 answers

lock(_locker) is displayed in your code, which means you are in a multi-threaded environment. Can you verify that nothing else has NULLing your variable? that is, that everything else also correctly calls lock(_locker) ?

+2
source

Maybe your NULL is in res.EnergyServiceLevel .

+1
source

src.EnergyServiceLevel.ServiceGrade may be null

Moving the mouse over each link will definitely show you what is null.

0
source

All Articles