0

Null 参照の例外は常に一般的にあいまいであり、いくつでもそれを生成できることを理解していますが、ここでアイデアが不足しています...何が起こっているのかは、レシピカテゴリのリンクをクリックしようとすると、次のエラーが発生することです:

NullReferenceException: Object reference not set to an instance of an object.
Company.BusinessLayer.Recipes.SearchManager.GetRecipes(Int64 folderId, String searchTerm, Int32 pageIndex, Int32 pageSize, Int32& categoryCount, Int32& pageCount) +1020
TargetInvocationException: Exception has been thrown by the target of an invocation.
Comapny.Website.Controls.Recipes.CategoryControl.ods_Selected(Object sender, ObjectDataSourceStatusEventArgs e) +940


System.Web.UI.WebControls.ObjectDataSourceView.OnSelected(ObjectDataSourceStatusEventArgs e) +103
   System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance) +431
   System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1953
   System.Web.UI.WebControls.BaseDataList.GetData() +56
   System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean useDataSource) +177
   System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +64
   System.Web.UI.WebControls.BaseDataList.DataBind() +55
   Company.Website.Controls.Recipes.CategoryControl.OnPreRender(EventArgs e) +138
   System.Web.UI.Control.PreRenderRecursiveInternal() +103
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496

ただし、何がnullとして処理されるのかわかりません。これは SearchManager.cs のコードと、エラー メッセージが参照する GetRecipes メソッドです。データはすべてデータベースにあり、null になるものはありません。このプロジェクトが依存しているアセンブリをアップグレードする前は、一度は機能していました。変更された唯一のことは、アセンブリが int の代わりに int64 を使用することですが、いまいましいプロジェクトをビルドするには、それらを int にキャストする必要がありました。この状況で私が思うだろうが、それは問題ではないはずです。ここで空気をつかんでいるのかわかりません。

public ContentBase[] GetRecipes(long folderId, string searchTerm, int pageIndex, int pageSize, out int categoryCount, out int pageCount)
{
    ContentBase[] mergedResultset = null;

    IndexSearch idx = new IndexSearch();

    idx.XmlConfigId = 8;
    idx.FolderId = folderId;
    idx.Recursive = true;


    // if a search term is provided, search this term
    if (!string.IsNullOrEmpty(searchTerm))
    {
        searchTerm = searchTerm.Replace("'", "''");

        // INDEX SEARCH BASED ON INGREDIENTS
        Ektron.Cms.Controls.IndexSearch.SearchPram paramTerm = new Ektron.Cms.Controls.IndexSearch.SearchPram();
        paramTerm.DataType = Ektron.Cms.Common.EkEnumeration.XMLDataType.String;
        paramTerm.SearchType = Ektron.Cms.Common.EkEnumeration.XMLSearchRangeType.Contains;
        paramTerm.XPath = "/root/Ingredients";
        paramTerm.Value1 = searchTerm;
        idx.AddParm(paramTerm);

        idx.Search();
        mergedResultset = (ContentBase[])idx.EkItems;

        // CUSTOM SEARCH BASED ON TITLE
        FolderData[] childFolders = GetChildFolders(folderId, true);

        // Search based on title
        Ektron.Cms.API.Search.SearchManager search = new Ektron.Cms.API.Search.SearchManager();
        Ektron.Cms.ContentSearchCondition conditionAll = new Ektron.Cms.ContentSearchCondition();
        conditionAll.setType = EkEnumeration.SearchType.AND;

        ContentSearchCondition condFolderIds = new ContentSearchCondition();
        // add condition folder Id for current folder and for each sub folder
        condFolderIds.setType = EkEnumeration.SearchType.OR;
        AddFolderIdCondition(condFolderIds, folderId);
        foreach (FolderData fd in childFolders)
        {
            AddFolderIdCondition(condFolderIds, fd.Id);
        }
        conditionAll.AddCondition(condFolderIds);

        ContentSearchCondition condTerm = new ContentSearchCondition();
        condTerm.setType = EkEnumeration.SearchType.LIKE;
        condTerm.setValue = searchTerm;
        condTerm.setVariable = "content.content_title";
        conditionAll.AddCondition(condTerm);

        ContentSearchCondition condType = new ContentSearchCondition();
        condType.setType = EkEnumeration.SearchType.EQUAL;
        condType.setValue = 8;
        condType.setVariable = "content.xml_config_id";
        conditionAll.AddCondition(condType);

        ContentData[] contents = search.Execute(conditionAll);


        mergedResultset = MergeResultsets(idx.EkItems, contents, "/Recipes/Detail.aspx");

        // rewrite all quicklinks with url alias
        IDictionary<int, string> aliases = new UrlAliasApi().GetUrlAliasesByType(Company.DataLayer.Enumeration.UrlAliasType.Recipe);
        foreach (ContentBase recipe in mergedResultset)
        {
            // if alias exists, overwrite quicklink!
            string alias;
            if (aliases.TryGetValue((int)recipe.Id, out alias))
            {
                recipe.QuickLink = alias;
            }
        }

    }
        // otherwise search based without parameter (fake param)
    else
    {
        // if category recipes not already in cache, get them through ektron, otherwise, just get them from cache
        if (RecipeCategoryCache.Current.Categories.ContainsKey((int)folderId) && RecipeCategoryCache.Current.Categories[(int)folderId] != null && RecipeCategoryCache.Current.Categories[(int)folderId].Length > 0)
        {
            mergedResultset = RecipeCategoryCache.Current.Categories[(int)folderId];
        }
        else
        {
            Ektron.Cms.Controls.IndexSearch.SearchPram param = new Ektron.Cms.Controls.IndexSearch.SearchPram();
            param.DataType = Ektron.Cms.Common.EkEnumeration.XMLDataType.Boolean;
            param.SearchType = Ektron.Cms.Common.EkEnumeration.XMLSearchRangeType.True;
            param.XPath = "/root/Viewable";
            idx.AddParm(param);

            idx.Search();
            mergedResultset = (ContentBase[])idx.EkItems;

            // rewrite all quicklinks with url alias
            IDictionary<int, string> aliases = new UrlAliasApi().GetUrlAliasesByType(Company.DataLayer.Enumeration.UrlAliasType.Recipe);
            foreach (ContentBase recipe in mergedResultset)
            {
                // if alias exists, overwrite quicklink!
                string alias;
                if (aliases.TryGetValue((int)recipe.Id, out alias))
                {
                    recipe.QuickLink = alias;
                }
            }

            RecipeCategoryCache.Current.Categories[(int)folderId] = mergedResultset;
        }
    }

    categoryCount = mergedResultset.Length;

    // if page size provided is 0, No paging,
    // if more than 0, calculate paging
    if (pageSize > 0 && mergedResultset.Length > 0)
    {
        PagingHelper pgHelper = new PagingHelper();
        pgHelper.CalculatePagingInfo(pageIndex, categoryCount, pageSize);

        pageCount = pgHelper.NumberOfPages;

        List<ContentBase> lst = new List<ContentBase>();

        for (int i = pgHelper.StartIndex; i < pgHelper.EndIndex; i++)
        {
            lst.Add(mergedResultset[i]);
        }

        return lst.ToArray();

    }
    else
    {
        pageCount = 1;
        return mergedResultset;
    }

}
4

3 に答える 3

1

エラー処理コードの TON を追加する必要があります。「if (x == null)」タイプのステートメントの単一の「if (x != null)」はありません。より防御的になるようにすべてのコードを書き直す必要があります。

はい、この特定のエラーを見つけるには、プログラムをステップ実行する必要があります。そして、どこで爆発するかを見てください。しかし、この 1 つの問題を修正したとしても、多くのエラー チェック コードを追加しない限り、コードが 5 分後に別の場所で爆発しないという保証はありません。

このメソッド全体で外部コードを呼び出しているようです。基本的に、そこから返されるすべてのものを検証する必要があります。外部コードが常に有効なデータを返すことを信頼しているようです。

于 2010-11-04T21:00:50.473 に答える
0

そこにいてコードを掘り下げることができなければ、これはほとんど不可能です。私があなたに言うことができるのは、nullであるサブオブジェクトを探すことです。たとえば、Carオブジェクトがあるとします。

class Car(){
string Color;
string Make;
string Model;
Passenger passenger; // Passenger is a another object defined elsewhere
}

そして、コードのある時点で、次のようなことをします...

車car=new Car();

次に、それをどこかで使用し、コードの途中で、何かがPassengerのプロパティにアクセスしようとしますが、PassengerはNULLです。問題は、より高いレベルで、車を見ると、車がnullではないことがわかるので、問題ないと思います。理解?

だから...あなたの複雑なオブジェクトにヌルがないかチェックして、それがどこにあるかを見てください。ところで、広く定義されているコード内のステートメントをキャッチしようとすると、この種のエラーが発生し、見つけるのが難しくなります...

于 2010-11-04T20:53:31.713 に答える
0

関数の先頭にブレークを設定し、段階的にデバッグして変数をチェックしてみませんか?これは、null参照例外の理由を理解するための最も簡単な方法です。

于 2010-11-04T20:50:57.210 に答える