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

8
  • 2
    Could you copy the code into your post instead of posting an image? The text is too small to read. Commented May 8, 2011 at 15:15
  • 2
    Please post the exception details as well. Commented May 8, 2011 at 15:20
  • 1
    possible duplicate of What is a NullReferenceException in .NET? Commented May 8, 2011 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 InnerExceptions? Commented May 8, 2011 at 15:30
  • 1
    @hammar: you can watch the image directly to have a full-scale view. Commented May 8, 2011 at 15:32

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

3 Comments

+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.
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.
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.
1

Maybe your NULL is at res.EnergyServiceLevel.

1 Comment

This wouldn't cause a NullReferenceException
0

src.EnergyServiceLevel.ServiceGrade may be null

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

2 Comments

This wouldn't cause a NullReferenceException
assigning a null value is perfectly legal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.