What to do when SPContext is null but you need it?

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#…

1