Search code examples
c#.netasp.netglobalizationnullreferenceexception

.NET: NullReferenceException when applying Culture Infos


this error is driving me nuts. Im building a website with the option to change between multiple languages.

Heres the (very) basic structure of the site:
Default.aspx (with CodeBehind-File)
Shared/Default.Master (with CodeBehinde-File)
App_Code/BasePage.cs
App_LocalResources/Default.aspx.en.resx

Default.aspx uses the Masterpage.
BasePage.cs: The BasePage class derivates from System.Web.Ui.Page (public class BasePage : Page)
Default.aspx.cs: Default derivates from BasePage (public partial class Default : BasePage)

All that works well.

Then I tried to override the InitializeCulture() Method to make it possible to change the language of the site. The culture should be in two-letter form (so "en" not "en-US" etc.).

This is how the BasePage.css File looks like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web.UI;
using System.Threading;
using System.Globalization;

namespace Web
{
public class BasePage : Page
{
    protected override void InitializeCulture()
    {
        LanguageQS = Request.QueryString["l"].ToString();

        // Language
        if( !String.IsNullOrEmpty( LanguageQS ) )
        {
            // Validate the QueryString Value
            string[] LanguagesArray = ConfigurationManager.AppSettings["Languages"].ToString().Split( ',' );
            if( LanguagesArray.Contains( LanguageQS ) )
            {
                Session["Language"] = LanguageQS;
                cultureString = LanguageQS;
            }
        }
        else if( Session.IsNewSession || String.IsNullOrEmpty( Session["Language"].ToString() ) )
        {
            // New Session, set default Language
            cultureString = ConfigurationManager.AppSettings["DefaultLanguage"].ToString();
        }
        else
        {
            // Get language from session
            cultureString = Session["Language"].ToString();
        }

        // Set the language
        try
        {

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture( cultureString );
            Thread.CurrentThread.CurrentUICulture = new CultureInfo( cultureString );
        }
        catch { throw; }

        base.InitializeCulture();
    }

    private string _culture;
    public string cultureString
    {
        set { this._culture = value; }
        get { return this._culture; }
    }

    private string _languageQS;
    public string LanguageQS
    {
        set { this._languageQS = value; }
        get { return this._languageQS; }
    }
}

}

Now I get the following error when I open the Page:

Line 1:  <%@ Page Language="C#" MasterPageFile="~/Shared/Default.Master" Inherits="Default" meta:resourcekey="PageResource" Codebehind="Default.aspx.cs" AutoEventWireup="True" %>

System.NullReferenceException: Object reference not set to an instance of an object.

I traced the error down to these two rows:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture( cultureString );
Thread.CurrentThread.CurrentUICulture = new CultureInfo( cultureString );

If I remove them all works well again.
But even if I put

cultureString = "en";

or

cultureString = "en-US";

right in front of those lines I get that NullException.

Anyone got a hint?

Im pretty new to .NET-Development and cant find whats causing this error.


Solution

  • Thanks everyone.

    My biggest culpa is not checking the debugger before posting here. :(

    Debugger told me that

    LanguageQS = Request.QueryString["l"].ToString();
    

    failed, because no QuerySting was given and ToString() cant convert null to a string.

    Fixed that.

    Thanks for the hint on the return paths. Fixed that as well.