tags:

views:

95

answers:

5

Hi,

Have an app which has been running fine for a while. All of a sudden it has started throwing intermitent "Object reference not set...." errors at a certain line of code on the product server.

I started the app in Visual Studio and debugged it with a breakpoint at the offending line of code and have replicated a really strange behaviour.

Initially when VS breaks at the line of code breakpoint, the variable is NULL but if I wait 5 secs or so then the variable is no longer NULL. This can be sped up by calling the variable in the Immediate Window in VS which causes the NULL field to populate or resolve instantly. This is the exact intermitent nature of the error on the production server.

Any ideas where to start? The data is loaded into the variable before hitting the method but for some reason there is a lag in referencing the variable in memory. Heaps of available memory on dev and production servers.

Really strange...need help to locate a place to start to resolve it.

Thanks in advance.

Info : .NET 3.5, VS 2008

Code :

public static List<Model> CreateModel(List<subModelA> subModelAs, List<subModelB> subModelBs, int duration, bool isGroup) {
            List<Model> result = new List<Model>();

            try {
                if (subModelAs != null && subModelAs.Count > 0) {
                    if (subModelBs != null && subModelBs.Count > 0) {

                        subModelBs.ForEach(b => {
                            subModelA a = subModelAs.Find(x => x.id == b.comp[0].subModelAComp.subModel.id);
                            result.Add(CreateNewModel(a, b, duration));
                        });
                    }
                }
            }
            catch (Exception ex) {
                throw ex;
            }
            return result;
        }

The following line is the source of the issue :

subModelA a = subModelAs.Find(x => x.id == b.comp[0].subModelAComp.subModel.id);

If Sometimes b is NULL and other times subModelAComp is NULL. If i breakpoint this line and in the Immediate Window if i execute "b.comp[0].subModelAComp" it is NULL, i then execute "b" and then execute "b.comp[0].subModelAComp" again and it is no longer NULL. The call to "b" seems to force the "subModelAComp" to not be NULL. There is no code in the getters which could cause this change.

* The application is not threaded. No multi-threading implemented **

+4  A: 

Without seeing any code, it's pretty hard to say, but two possibilities:

  • Is this definitely a variable, or might it be a property? If it's a property, evaluating it once may give a non-null value, but the second evaluation could be null

  • Are there multiple threads involved? If so, there are two potential problems:

    • If the data is being set in one thread but read in another, the "setting" thread may have written it, but without the "reading" thread reading it, due to caching etc. There are various ways around this - the simplest being to always acquire a lock before accessing shared data. (Using the same lock for every access to the same piece of data.)
    • Another thread could be setting the value to null - so you'd see it be non-null, and then later null

Any code you could show us would help.

Jon Skeet
Thanks Jon, seems like your first suggestion in that it is property getters causing the issue but the getter has no internal logic except for a standard return. The app is not threaded.
teck
@teck: Then it really doesn't make much sense to me... Can you post any code?
Jon Skeet
edited the original post to add the offending code. im stumped, never seen this issue before.
teck
+1  A: 

Hm, my first guesses would be:

  • Is your application multi-threaded? Could another thread be accessing the variable? During debugging, check the “Threads” debug window and try freezing all the threads except the current one, then see if the variable contents still change unexpectedly.

  • Is it really a variable? Or could it be a property getter that has crazy side-effects? The debugger would trigger those side-effects too when you query the property from the “Watch” window or the “Immediate” window.

Timwi
Thanks for the prompt ideas.1) The application is not multithreaded2) It is not an "actual" variable but properties. However the getter is just a single return with no additional logic.The code has :varA.colB[0].varC.varD.varEthe line of code is accessing varE which belongs to varD. However its either varA or varC which is causing the NULL ref error. If the code exits the method and then the same variables are checked, the data is there in the variables. Ive tried rebuilding the app and still the same issue exists.
teck
+1  A: 

If it's a property, put a breakpoint on the property setter, so you can view the stack trace to see what is setting it to null.

David_001
teck
A: 

Are ienumerables used anywhere? I've run into situations like this whem doing some funky stuff that can mess up the lazy execution of yield statements.

Rob Galanakis
A: 

Any ideas where to start? The data is loaded into the variable before hitting the method but for some reason there is a lag in referencing the variable in memory.

To me, that assertion smells.

Are you sure that the data is loaded into the variable before hitting the method? Perhaps you haven't written any multithreading code, but you may be using some .NET class with asynchronous behavior (events, etc.) I would take this assumption out and debug from there. (Meaning, I'd start by setting a breakpoint before the .ForEach calls, or even before CreateModel is called and check each "b" in the list manually)

Jeff Meatball Yang