"Object reference not set to an instance of an object" after installing 9.3 and my assemblies

I broke my local installation of Sitecore so I decided to install a clean version of 9.3. I added my own project back in but now I'm getting the errors above, any ideas? Happy to provide code snippets if needed. 

@using Sitecore.Mvc
@using Sitecore.Mvc.Analytics.Extensions
@using Sitecore.Mvc.Presentation
@using TAC.Utils
@using TAC.Utils.Helpers
@model RenderingModel
@{
    Layout = "~/Views/Guard/Layouts/_Head.cshtml";

    var decorationBannerUrl = Html.SitecoreMediaUrl(Statics.FieldNames.DecorationBanner);
    var mobileBannerUrl = Html.SitecoreMediaUrl("MobileBanner");


    var headerCssClass = "page-header";
    var headerBackgroundStyle = "";
    var mobileBackgroundStyle = "";

    if (!String.IsNullOrEmpty(decorationBannerUrl))
    {
        headerCssClass = headerCssClass + " " + "bg-media bg-parallax";
        headerBackgroundStyle = string.Format("background-image: url('{0}')", decorationBannerUrl);
    }
    if (!String.IsNullOrEmpty(mobileBannerUrl))
    {
        headerCssClass = headerCssClass + " " + "bg-media bg-parallax";
        mobileBackgroundStyle = string.Format("background-image: url('{0}')", mobileBannerUrl);
    }
    else
    {
        if (!String.IsNullOrEmpty(decorationBannerUrl))
        {
            // no special mobile image exists so fall back to the desktop header image
            headerCssClass = headerCssClass + " " + "bg-media bg-parallax";
            mobileBackgroundStyle = string.Format("background-image: url('{0}')", decorationBannerUrl);
        }
    }

}
<div id="main-container">
    <header class="siteheader">
        @Html.Sitecore().Placeholder(Statics.PlaceholderKeys.HeaderNavigation)
    </header>
    <main role="main"> 
        <div class="visible-lg visible-md">
            @using (Html.BeginHeader(new { @class = headerCssClass, style = headerBackgroundStyle }))
            {
                @Html.Sitecore().Placeholder(Statics.PlaceholderKeys.HeaderContent)
            }
        </div>
        <div class="visible-sm visible-xs">
            <div>
            @using (Html.BeginHeader(new { @class = headerCssClass, style = mobileBackgroundStyle }))
            {
            }
            </div>
            @Html.Sitecore().Placeholder(Statics.PlaceholderKeys.HeaderContent)
        </div>
        <section class="section section-full">
            @Html.Sitecore().DynamicPlaceholder(Statics.PlaceholderKeys.MainContent)
        </section>
    </main>
    <div>
                @Html.Sitecore().Placeholder(Statics.PlaceholderKeys.FooterContent)          
    </div>
</div>

using System;
using System.Text.RegularExpressions;
using Sitecore.Data.Items;
using Sitecore.Links;

namespace TAC.Utils.Helpers
{
    public class CustomLinkProvider : Sitecore.Links.LinkProvider
    {
        public override string GetItemUrl(Item item, UrlOptions options)
        {
            string query = String.Format("/*/system/Aliases//*[@@templateid='{0}' and contains(@Linked item, '{1}')]", Sitecore.TemplateIDs.Alias, item.ID);
            Sitecore.Data.Items.Item alias = Sitecore.Context.Database.SelectSingleItem(query);
            string aliasUrl = string.Empty;
            if (alias != null)
            {
                aliasUrl = base.GetItemUrl(alias, options);
                //remove the system part of the url that is returned.
                aliasUrl = Regex.Replace(aliasUrl, @"/sitecore/system/aliases", "", RegexOptions.IgnoreCase);
                aliasUrl = aliasUrl.Replace("-", " ");
                return aliasUrl;
            }
            else
            {
                return base.GetItemUrl(item, options);
            }
        }
    }
}

I'm not sure why this is suddenly an issue as there were no issues with these views/assemblies before the update.

  • Looks like you have a null reference exception in your TAC.Utils.helpers.CustomLinkProvider
    I assume the CustomLinkProvider is set in the config as LinkManager?

    You can try to attach debugger visual studio to find out why, or take a look at your view, are you trying to create an url of a null item object?
  • In reply to Jan Bluemink:

    Code added above, kinda stumped currently.
  • In reply to Paul Corderoy:

    Likely your Model.Item is null, where you have the var Canonical = new CLp.GetItemUrl(Model.Item ....

    make sure Model.Item contain a item.  empty/not existing datasource? since it is about an Canonical  I assume you need the current context, try Sitecore.Context.Item instead of Model.Item

  • In reply to Jan Bluemink:

    Same issue as before:

    Line 48: var canonical = CLP.GetItemUrl(Sitecore.Context.Item, options);
  • This is due to a change in the LinkProvider for 9.3. It has changed internally which causes a problem when you instantiate a LinkProvider directly.

    Read my blog entry here for further background information (Search for LinkProvider).

    There are two options - manually call the initialize method with the appropriate configuration key:

    var customLinkProvider = new CustomLinkProvider();
    customLinkProvider.Initialize("customConfig",newNameValueCollection());
    return customLinkProvider.GetItemUrl(item);

    Or use the standard link provider:

    LinkManager.GetItemUrl(item);

     

    Hope this helps

     

    Matt Fletcher | Freelance MVP 2020, Sitecore Developer & Technical Architect 

    @sitecorium