views:

167

answers:

2

I'm getting NullReferenceExeceptions at seemly random times in my application and can't track down what could be causing the error.

I'll do my best to describe the scenario and setup.

Any and all suggestions greatly appreciated!

  • C# .net 3.5 Forms Application, but I use the WebFormRouting library built by Phil Haack (http://haacked.com/archive/2008/03/11/using-routing-with-webforms.aspx) to leverage the Routing libraries of .net (usually used in conjunction with MVC) - intead of using url rewriting for my urls.

  • My database has 60 tables. All Normalized. It's just a massive application. (SQL server 2008)

  • All queries are built with Linq to SQL in code (no SP's). Each time a new instance of my data context is created. I use only one data context with all relationships defined in 4 relationship diagrams in SQL Server.

  • the data context gets created a lot. I let the closing of the data context be handled automatically. I've heard arguments both sides about whether you should leave to be closed automatically or do it yourself. In this case I DONT do it myself.

  • It doesnt seem to matter if I'm creating a lot of instances of the data context or just one.

For example, I've got a vote-up button. with the following code, and it errors probably 1 in 10-20 times.

protected void VoteUpLinkButton_Click(object sender, EventArgs e)
{
    DatabaseDataContext db = new DatabaseDataContext();

    StoryVote storyVote = new StoryVote();
    storyVote.StoryId = storyId;
    storyVote.UserId = Utility.GetUserId(Context);
    storyVote.IPAddress = Utility.GetUserIPAddress();
    storyVote.CreatedDate = DateTime.Now;
    storyVote.IsDeleted = false;

    db.StoryVotes.InsertOnSubmit(storyVote);
    db.SubmitChanges();

    // If this story is not yet published, check to see if we should publish it.  Make sure that
    // it is already approved.
    if (story.PublishedDate == null && story.ApprovedDate != null)
    {
        Utility.MakeUpcommingNewsPopular(storyId);
    }

    // Refresh our page.
    Response.Redirect("/news/" + category.UniqueName + "/"
        + RouteData.Values["year"].ToString() + "/"
        + RouteData.Values["month"].ToString() + "/"
        + RouteData.Values["day"].ToString() + "/"
        + RouteData.Values["uniquename"].ToString());
}

The last thing I tried was the "Auto Close" flag setting on SQL Server. This was set to true and I changed to false. Doesnt seem to have done the trick although has had a good overall effect.

Here's a detailed that wasnt caught. I also get slighly different errors when caught by my try/catch's.

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. --->
System.NullReferenceException: Object reference not set to an instance of an object. at
System.Web.Util.StringUtil.GetStringHashCode(String s) at
System.Web.UI.ClientScriptManager.EnsureEventValidationFieldLoaded() at
System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) at
System.Web.UI.WebControls.TextBox.LoadPostData(String postDataKey, NameValueCollection postCollection) at
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) at
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) --- End of inner exception stack trace --- at
System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at
System.Web.UI.Page.ProcessRequest(HttpContext context) at
ASP.forms_news_detail_aspx.ProcessRequest(HttpContext context) at
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

HELP!!!

A: 

From the stack trace it doesn't look like anything to do with LINQ. Looking at System.Web in Reflector it would appear that Page.RequestViewStateString is null somehow. I'd confirm that in the debugger when the exception occurs and try to track back what happens from there (using Reflector).

Evgeny
Thanks Evgeny, taking a look now.
Shane
A: 

oh boy, fingers crossed.

came accross this. http://forums.asp.net/t/1170677.aspx

where it talks about setting: enableEventValidation="false" in the page or in web.config for all pages.

I would prefer not having to do this and technically it doesnt 'fix' the error, just avoids it by the looks of things.

On the live site, i'd be happy with a bandaid for now.

thanks Evgeny, you put me on the right track. I suppose I suspected Linq to SQL wrongly and it should have been perhaps more obvious from the stacktrace.

Unfortunately (or fortunately), I'm trying a lot of 'new stuff', so lots of not yet seen errors.

Shane