Object reference not set to an instance of an object. when c

Posted by Community Admin on 03-Aug-2018 19:18

Object reference not set to an instance of an object. when creating a Hierarchical Taxonomies and Taxons

All Replies

Posted by Community Admin on 29-Apr-2012 00:00

Hi there,

I am trying to export an existing site to a new platform using Sitefinity. So, I am trying to export a list of categories and subcategories and in which I am using this code :

private static void ImportCategories()
        
            TaxonomyManager manager = TaxonomyManager.GetManager();
            var taxonomy = manager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Categories").SingleOrDefault();
 
            if (taxonomy == null) return;
 
            var listCateg = ImportCategAndSubCateg.GetListCategories();
            foreach (var itemCateg in listCateg)
            
                var rootTaxonCateg = manager.CreateTaxon<HierarchicalTaxon>();
                rootTaxonCateg.Name = itemCateg.Name;
                rootTaxonCateg.Title = itemCateg.Name;
                rootTaxonCateg.Description = itemCateg.Description;
 
                taxonomy.Taxa.Add(rootTaxonCateg);
                var listSubCateg = ImportCategAndSubCateg.GetSubCategPerCateg(itemCateg.ID);
                foreach (var itemSubCateg in listSubCateg)
                
                    var taxonSubCateg = manager.CreateTaxon<HierarchicalTaxon>();
                    taxonSubCateg.Title = itemSubCateg.Name;
                    rootTaxonCateg.Subtaxa.Add(taxonSubCateg);
 
                    var listChildSubCateg = ImportCategAndSubCateg.GetSubCategPerSubCateg(itemCateg.ID, itemSubCateg.SubcategParentId);
                    foreach (var itemChildSubCateg in listChildSubCateg)
                    
                        var taxonChildSubCateg = manager.CreateTaxon<HierarchicalTaxon>();
                        taxonChildSubCateg.Title = itemChildSubCateg.Name;
                        taxonSubCateg.Subtaxa.Add(taxonChildSubCateg);
                    
                
            
 
            manager.SaveChanges();
        

but this code fails when it comes to persist the data and I have this error :

Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
 
Source Error:
 
Line 56:            
Line 57:
Line 58:             manager.SaveChanges();
Line 59:        
Line 60:    
 
 
Source File: C:\Code\SmartBizMart\SmartBizMart\Widgets\Migration\MigrationTool.ascx.cs    Line: 58
 
Stack Trace:
 
[NullReferenceException: Object reference not set to an instance of an object.]
   Telerik.Sitefinity.Security.AuthorizationPermissionProvider.AuthorizeInput(IMethodInvocation input) +3234
   Telerik.Sitefinity.Security.SitefinityAuthorizationCallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) +274
   Telerik.Microsoft.Practices.Unity.InterceptionExtension.HandlerPipeline.Invoke(IMethodInvocation input, InvokeHandlerDelegate target) +221
   Telerik.Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior.Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) +199
   Telerik.Microsoft.Practices.Unity.InterceptionExtension.InterceptionBehaviorPipeline.Invoke(IMethodInvocation input, InvokeInterceptionBehaviorDelegate target) +221
   DynamicModule.ns.Wrapped_OpenAccessTaxonomyProvider_4a8e5586d1fd436e8695ba4d12297e8e.CommitTransaction() +181
   Telerik.Sitefinity.Data.ManagerBase`1.SaveChanges() +136
   SitefinityWebApp.Widgets.Migration.MigrationTool.ImportCategories() in C:\Code\SmartBizMart\SmartBizMart\Widgets\Migration\MigrationTool.ascx.cs:58
   SitefinityWebApp.Widgets.Migration.MigrationTool.btnOk_Click(Object sender, EventArgs e) in C:\Code\SmartBizMart\SmartBizMart\Widgets\Migration\MigrationTool.ascx.cs:23
   Telerik.Web.UI.RadButton.OnClick(ButtonClickEventArgs e) +126
   Telerik.Web.UI.RadButton.RaisePostBackEvent(String eventArgument) +265
   Telerik.Web.UI.RadButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +42
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
 
 
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

Could you please tell what I am doing wrong?
Thank you

Posted by Community Admin on 19-Jul-2012 00:00

Did anyone ever find a solution to this.  I upgrade to 5.1 and am getting this error as well?

Posted by Community Admin on 19-Jul-2012 00:00

Hi Victor,

I have submitted this issue to support and got this answer


var manager = TaxonomyManager.GetManager();
           manager.Provider.SuppressSecurityChecks = true;
           var categoriestaxonomy = manager.GetTaxonomy<HierarchicalTaxonomy>(TaxonomyManager.CategoriesTaxonomyId);
 
           var rootTaxonCateg = manager.CreateTaxon<HierarchicalTaxon>();
           rootTaxonCateg.Title = "Language Groups";
           rootTaxonCateg.Name = "Language Groups";
           rootTaxonCateg.Description = "Language groups description";
           rootTaxonCateg.Taxonomy = categoriestaxonomy;
           categoriestaxonomy.Taxa.Add(rootTaxonCateg);
 
           var taxonSubCateg = manager.CreateTaxon<HierarchicalTaxon>();
           taxonSubCateg.Title = "Languages";
           taxonSubCateg.Name = "Languages";
           taxonSubCateg.Description = "Languages description";
           taxonSubCateg.Parent = rootTaxonCateg;
           taxonSubCateg.Taxonomy = categoriestaxonomy;
           rootTaxonCateg.Subtaxa.Add(taxonSubCateg);
 
           var taxonChildSubCateg = manager.CreateTaxon<HierarchicalTaxon>();
           taxonChildSubCateg.Title = "Dialects";
           taxonChildSubCateg.Name = "Dialects";
           taxonChildSubCateg.Description = "Dialects description";
           taxonChildSubCateg.Parent = taxonSubCateg;
           taxonChildSubCateg.Taxonomy = categoriestaxonomy;
           taxonSubCateg.Subtaxa.Add(taxonChildSubCateg);
 
           manager.SaveChanges();

I tried it and it works.
Hope it will help you as well.

This thread is closed