Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jason Espin 368 posts 1335 karma points
    May 01, 2014 @ 18:38
    Jason Espin
    0

    SaveAndPublishWithStatus give 'Object reference not set to an instance of an object' exception

    Hi all,

    I'm currently getting the error above when running the SaveAndPublishWithStatus method of a content service object. I've stepped through my code in Visual Studio to ensure that none of the items I am passing in the object are null but it is within this Umbraco defined method that the problem is occuring and not my own code. Please see my overall method below:

    public void GetCountries(CompressionSoapHeader compressionSoapHeader, ManagedUserHeader managedUserHeader)
    {
      StaticDataWebServiceSoapClient webService = null;
      webService = new StaticDataWebServiceSoapClient();
      IContentService contentService = ApplicationContext.Current.Services.ContentService;
      bool success = false;
      try
      {
        //Construct Service Request
        var homePageId = contentService.GetRootContent().ElementAt(0).Id;
        var continentArray = contentService.GetByLevel(3).Where(x => x.ContentType.Alias == "continent");
        foreach (var continent in continentArray)
        {
          var continentId = continent.GetValue<int>("continentId");
          var countries = webService.GetCountryList(ref compressionSoapHeader, ref managedUserHeader, continentId, string.Empty);
          foreach (var country in countries)
          {
            if(country != null){
              var id = country.Id;
              var name = country.Name;
              var notes = country.Notes;
              var description = country.DescriptionData;
              var descConcatenated = String.Empty;
              if (description != null)
              {
                try
                {
                  foreach (var desc in description)
                  {
                    if (desc != null)
                    {
                      descConcatenated = descConcatenated + "<br/>" + desc.Notes;
                    }
                  }
                }
                catch (NullReferenceException e)
                {
                  string errorHandler = Request.QueryString["handler"];
                  ExceptionUtility.LogException(e, errorHandler);
                  Server.ClearError();
                }
              }
    
              try
              {
                var cs = contentService.CreateContent(country.Name, continentId, "country");
                cs.SetValue("mainPageContent", descConcatenated);
                cs.SetValue("pageTitle", country.Name);
                cs.SetValue("countryNotes", country.Notes);
                cs.SetValue("countryId", country.Id);
                cs.SetValue("continentId", continentId);
                contentService.SaveAndPublishWithStatus(cs);
              }
              catch(Exception e)
              {
                string errorHandler = Request.QueryString["handler"];
                ExceptionUtility.LogException(e, errorHandler);
                Server.ClearError();
              }
            }
          }
        }
      }
      finally
      {
        if (!success && webService != null)
        {
          webService.Abort();
        }
      }
    }
    

    Any ideas what could be causing this? According to my error log:

    ********** 01/05/2014 17:32:29 **********
    Exception Type: System.NullReferenceException
    Exception: Object reference not set to an instance of an object.
    Source: 
    Stack Trace: 
         at Umbraco.Core.Services.ContentService.IsPublishable(IContent content, Boolean checkCurrent)
         at Umbraco.Core.Services.ContentService.IsPublishable(IContent content)
         at Umbraco.Core.Services.ContentService.CheckAndLogIsPublishable(IContent content)
         at Umbraco.Core.Services.ContentService.SaveAndPublishDo(IContent content, Int32 userId, Boolean raiseEvents)
         at Umbraco.Core.Services.ContentService.SaveAndPublishWithStatus(IContent content, Int32 userId, Boolean raiseEvents)
         at JesperHannibalWebsite.umbraco.StaticDataService.GetCountries(CompressionSoapHeader compressionSoapHeader, ManagedUserHeader managedUserHeader) in c:\Users\jason\Documents\Visual Studio 2013\Projects\JesperHannibalWebsite\JesperHannibalWebsite\umbraco\StaticDataService.aspx.cs:line 152
    
Please Sign in or register to post replies

Write your reply to:

Draft