You need to track down where the error is occuring.
My method is to put debug statements at the entry and exit of my functions. I generally just print the function name, indented using _ (underscore) for each level of function it is going into.
From there, I catch any errors that happen, display them, (prevent DNN from handling it) and then look at my output and see what function was the last one that my debug went into.
It's a little trickier with it happening close to page load - but eliminate some pieces of code, try to get the error for a reasonable amount of time, if you get it, eliminate some more... if you don't (again, a lot of time may be required to be reasonably sure the error isn't coming) add back in some of the last code removed.
I normally remove large swatches of code, and then slowly add code back, same idea. Remove code, test, uncomment small chunks, test... repeat.
There isn't enough information to figure out the problem from what we have, for me anyway. You need to debug it and find the offending line(s) of code and continue from there.