Sometimes you stumble upon really strange errors when doing trivial things. When you dig deeper, sometimes it turns out the error comes from the lack of a HttpContext, for example when running some action through STSADM or a timer job.
Recently I faced a strange ArgumentNullException when creating a Content by Query Webpart in C# code. A feature activated event handler was supposed to add a new webpart to a page. Wiring everything up, testing the feature activation through the UI, everything was smooth. As a last step I made the feature hidden and tried to activate the feature through stsadm. And this is where I was left out of luck. STSADM only gave me a very helpful “Value cannot be null”. Digging deeper in the ULS log I caught a callstack which showed that there is some problem getting the SPContext.
Be aware that this trick only works when you are working with an SPWeb level object. When dealing with SPList or lower hierarchy items you need to go an other way.