Null reference error when using IContentManager.Create

Topics: Writing modules
Apr 12, 2012 at 9:33 PM
Edited Apr 12, 2012 at 9:39 PM

The error occurs on Line 81 of ContentToken.cs

 

foreach (var typePart in forContent.Data.ContentItem.TypeDefinition.Parts) {

I ran into this error when working through the advanced tutorials on Pluralsight.
 

It happens when running the following code:

 

using Contrib.Taxonomies.Models;
using Contrib.Taxonomies.Services;
using Orchard.ContentManagement;
using Orchard.Environment;

namespace Pluralsight.Movies.Handlers
{
    public class MovieFeatureEventHandler : IFeatureEventHandler {
        private readonly ITaxonomyService _taxonomyService;
        private readonly IContentManager _contentManager;

        public MovieFeatureEventHandler(ITaxonomyService taxonomyService, IContentManager contentManager) {
            _taxonomyService = taxonomyService;
            _contentManager = contentManager;
        }

        public void Installing(Orchard.Environment.Extensions.Models.Feature feature)
        {
        }

        public void Installed(Orchard.Environment.Extensions.Models.Feature feature)
        {
        }

        public void Enabling(Orchard.Environment.Extensions.Models.Feature feature)
        {
           
        }

        private void CreateTerm(TaxonomyPart genre, string genreName) {
            var term = _taxonomyService.NewTerm(genre);
            term.Name = genreName;
            _contentManager.Create(term, VersionOptions.Published);
        }

        public void Enabled(Orchard.Environment.Extensions.Models.Feature feature)
        {
            if (_taxonomyService.GetTaxonomyByName("Genre") == null)
            {
                var genre = _contentManager.New<TaxonomyPart>("Taxonomy");
                genre.Name = "Genre";
                _contentManager.Create(genre, VersionOptions.Published);

                CreateTerm(genre, "Action");
                CreateTerm(genre, "Adventure");
                CreateTerm(genre, "Animation");
                CreateTerm(genre, "Comedy");
                CreateTerm(genre, "Crime");
                CreateTerm(genre, "Documentary");
                CreateTerm(genre, "Drama");
            }
        }

        public void Disabling(Orchard.Environment.Extensions.Models.Feature feature)
        {
        }

        public void Disabled(Orchard.Environment.Extensions.Models.Feature feature)
        {
        }

        public void Uninstalling(Orchard.Environment.Extensions.Models.Feature feature)
        {
        }

        public void Uninstalled(Orchard.Environment.Extensions.Models.Feature feature)
        {
        }
    }
}

When we hit _contentManager.Create(term, VersionOptions.Published);, it makes it's way to ContentToken.cs where we get the null exception error.
I made a slight modification to line 80 of ContentToken.cs:
if (forContent != null && forContent.Data != null) {
and it seems to be doing the trick. Problem is, I am not sure if this is a bug
or if I am just doing something wrong. I appreciate your input.

Also, I am using the latest revision of the source code.

Thanks,
Jason