Q:

CrossListQueryCache().GetSiteData(contextWeb) throws NullReferenceException on second query?

I have an auqward problem that's been nagging me on and off for half a years time. It turns out, when I'm finally taking a real shot at it that my query throws a NullReferenceException the second time I run the script. The first time I ask for 1 row in return, and the second time I ask for three rows in return and I get "Object reference not set to an instance of an object" on GetSiteData(). 
I have no idea why?
Source code:
    protected DataTable QueryMyNews(string PropertyValue)
    {
        using (SPSite newsSite = new SPSite(ProtocolHelper.SetContextProtocol(SPContext.Current.Site, SettingsManager.GetSetting(SettingsEnum.NewsUrl))))
        {
            using (SPWeb contextWeb = newsSite.OpenWeb())
            {
                string viewfields = "<FieldRef Name=\"Title\" />";
                viewfields += "<FieldRef Name=\"FileRef\" />";
                viewfields += "<FieldRef Name=\"Comments\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"PublishingPageImage\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"ToppNyhet\" Nullable='TRUE'  />";
                viewfields += "<FieldRef Name=\"_ModerationStatus\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"Organisation\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"Created\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"Audience\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"ArticleStartDate\" Nullable='TRUE'/>";
                viewfields += "<FieldRef Name=\"{61cbb965-1e04-4273-b658-eedaa662f48d}\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"TaskDueDate\" Nullable='TRUE' />";

                CrossListQueryInfo query = new CrossListQueryInfo();
                query.FilterByAudience = true;
                query.ShowUntargetedItems = false;
                query.RowLimit = 3;
                query.WebUrl = SPContext.Current.Site.ServerRelativeUrl;
                query.Lists = "<Lists ServerTemplate=\"850\" />";
                query.Webs = "<Webs Scope=\"SiteCollection\" />";
                query.UseCache = false;
                query.ViewFields = viewfields;

                query.Query +=  "<Where>"+
                                    "<Or>"+
                                         "<Geq>"+
                                            "<FieldRef Name='TaskDueDate' />"+
                                            "<Value IncludeTimeValue='TRUE' Type='DateTime'><Today /></Value>"+
                                         "</Geq>"+
                                         "<IsNull>"+
                                            "<FieldRef Name='TaskDueDate' />"+
                                         "</IsNull>"+
                                    "</Or>"+
                               "</Where>";
                query.Query += "<OrderBy><FieldRef Name=\"Created\" Nullable=\"True\" Type=\"DateTime\" Ascending=\"FALSE\"/></OrderBy>";

                CrossListQueryCache myCache = new CrossListQueryCache(query);

                DataTable audienceAndForvaltning = myCache.GetSiteData(contextWeb); // Object refence not set to an instance of an object

A:

I had same problem.
After the deep investigation I found that inside the GetSiteData(contextWeb) method in same places used SPContext.Current.Web not contextWeb.
In my case case when SPContext.Current.Web is null I get an NullReferenceException
To fix it I wrote help class
public class ContextSwitcher : IDisposable
{
    private HttpContext _temp;
    private SPSite _site;
    private SPWeb _web;

    public ContextSwitcher(string url)
    {
        _site = new SPSite(url);
        _web = _site.OpenWeb();
        _temp = HttpContext.Current;

        HttpRequest request = new HttpRequest("", _web.Url, "");
        request.Browser = new HttpBrowserCapabilities();
        HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
        HttpContext.Current.Items["HttpHandlerSPWeb"] = _web;
    }

    public void Dispose()
    {
        HttpContext.Current = _temp;
        if (_web != null) _web.Dispose();
        if (_site != null) _site.Dispose();
    }
}

and rewrote my code using this class
using (ContextSwitcher contextSwitcher = new ContextSwitcher(webUrl))
        {
            CrossListQueryInfo query = new CrossListQueryInfo();
            query.Lists = "<Lists ServerTemplate=\"100\" />";
            query.Webs = "<Webs Scope=\"SiteCollection\" />";
            query.ViewFields = "<FieldRef Name=\"Title\" />";
            query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>TestTitle</Value></Eq></Where>";

            CrossListQueryCache cache = new CrossListQueryCache(query);

            DataTable resultTable = cache.GetSiteData(SPContext.Current.Web);
        }

This solved my problem.

