开发者

.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.


Thanks everyone.

Mea maxima culpa for 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.


for me below code works

private string _culture; 
    public string cultureString { 
        set { this._culture = value; }
        get { return this._culture; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        cultureString = "en";
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureString);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureString);
    }

only possibility for above code to fail if cultureString is set to null; so please check that scenario


Also set the Session["Langugage"] here :

// New Session, set default Language
        cultureString = ConfigurationManager.AppSettings["DefaultLanguage"].ToString();

Session["Language"] = cultureString


Because in the final else you are using it :)


In your code there are paths where cultureString is not assigned a value, e.g. where LanguageQS is not found in LanguagesArray.

Also ensure that Session["Language"] is not null before assigning it to cultureString.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜