4

Below is the code and the problematic line.

When I hover with the mouse on src.EnergyServiceLevel, it shows that it's null. How can that be if I'm checking for null in the previous line?

My guess was that maybe there are threads that making the problem so I've add a lock, but it didn't helped.

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;
}

The exception occurs at the res.EnergyServiceLevel = ... line in the above code.

Here's a screenshot of the exception occurring in debug mode:

Screenshot of exception while debugging

brianpeiris
  • 10,735
  • 1
  • 31
  • 44
Erez
  • 6,405
  • 14
  • 70
  • 124
  • 2
    Could you copy the code into your post instead of posting an image? The text is too small to read. – hammar May 08 '11 at 15:15
  • 2
    Please post the exception details as well. – Codo May 08 '11 at 15:20
  • 1
    possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders May 08 '11 at 15:24
  • 1
    I'm guessing that, since this is an extension method, that `ServiceLevelsGroup`/`ServiceLevelInfo` aren't classes in your control? In which case, the exception might be coming from within the `ServiceLevelInfo` constructor - what happens if you open the "View Detail..." link, and drill into appropriate `InnerException`s? – Damien_The_Unbeliever May 08 '11 at 15:30
  • 1
    @hammar: you can watch the image directly to have a full-scale view. – Arseni Mourzenko May 08 '11 at 15:32
  • @MainMa: Yes, but it's still better to post it in text as it's easier for people to help and it also indexes better in search engines later on. – hammar May 08 '11 at 15:34
  • 1
    Are you sure you are using debuger correclty? – Euphoric May 08 '11 at 15:36
  • 1
    Is EnergyServiceLevel a property? The getter might have a side-effect that is triggered by your debug mouse-over. – hsmiths May 08 '11 at 16:05

3 Answers3

2

Your code shows lock(_locker) - so it looks like you're in a multithreaded environment. Can you check that nothing else is NULLing your variable? i.e. that everything else is also calling lock(_locker) correctly?

Stuart
  • 66,722
  • 7
  • 114
  • 165
  • +1 A lock is only useful if you lock the setter as well. Although, since the OP is defining an extension method, he/she probably doesn't have access to the original code. – brianpeiris May 08 '11 at 15:36
  • I think you're the closest to the real thing, once i get to worl again i'll check maybe I didn't lock properly in other parts of the code. – Erez May 08 '11 at 19:32
  • thanks - if it's not a locking issue, then do use "view detail" and take a look at the call stack for the exception - it might just be somewhere inside the evaluation of the property. – Stuart May 08 '11 at 20:54
1

Maybe your NULL is at res.EnergyServiceLevel.

Erick Petrucelli
  • 14,386
  • 8
  • 64
  • 84
0

src.EnergyServiceLevel.ServiceGrade may be null

Moving mouse pointer to each reference will exactly show you which is null.

Dulini Atapattu
  • 2,735
  • 8
  • 33
  • 47