tags:

views:

90

answers:

2

This is my first post here, so go easy on me :) I'm having an odd problem with an ASP.Net page. I'm getting an object not set error message when assigning a string literal to a variable. Here's the error I'm getting.

[NullReferenceException: Object reference not set to an instance of an object.]
Project.Page.Page_Load(Object sender, EventArgs e) in C:\Project\Page.aspx.vb:10
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

And here is the code that the error is pointing to. The last line in this fragment is the line 10 the error is complaining about

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

  Dim teststring As String
  teststring = "test"

It seems it fails on accessing any reference type, even if it is declared locally in the load function. I originally found the problem when Request.IsSecureConnection was nulling out on me. I eventually discovered I could replace it with the above code and fail in the same way.

The problem does not always happen. It seems to be triggered by publishing an update from Visual Studio, or when I head home for the day and the server sits idle all night. One or two app pool recycles seems to clear it up, and the page functions correctly.

Any help would be greatly appreciated. I've been doing desktop development for a few years, but I'm fairly new to web development. So maybe this is something obvious I've got configured wrong. I'm currently using .Net 4 on IIS 6.

UPDATE

I was able to identify the line using the recommendation of copying the page, commenting everything out, and then uncommenting one line at a time. The line my test page started giving the error was this one

Dim StateName As String = State.Attribute(XName.Get("name")).Value

That code is in a foreach over an IEnumerable(of XElement). The stack trace still points to the string assignment, but all other stack traces are spot on, so I don't think it's a bad pdb.

Also, when the error occurred this time, no amount of recycling the app pool or stopping and starting it would fix the error. I finally got it working again by switching the application to a different app pool.

A: 

I don't see how that code could throw a null reference exception, are you sure it is not some other code, but the exception message is pointing to the wrong place becuase your pdb files are out of date. The most likely candidate for the code at fault based on your description is some code accessing an application variable that has become null becuase the app pool has recycled due to timeout or because you have republished the web app.

Ben Robinson
I agree that that code should not be able to throw a null reference. I checked the timestamps on my pdb and dll and they matched; although, I'm not sure if that is an accurate way to tell that they are matched.
cduncan
I agree this line of code can't throw a null reference exception. The error is somewhere else, but you need to narrow down where. Can you replicate the error on another blank page, where this is the only code on the page? If not then add things into your blank page from this one until the error occurs, and that is probably the real cause, assuming it is on this page at all.
James Gaunt
A: 

Try declaring the string as a constant... just to zoom in on the issue

Dim Const TESTSTR as String = "test"
Dim teststring As String
teststring = TESTSTR

If that doesn't work, move the Const declaration somewhere global. Maybe it's something to do with how/where .NET stores the string itself becoming invalid.

Try manually recycling the page every time you publish... maybe it's an issue of the pre-compiled code lingering after an update? I admit, that's a head-scratcher, a line like that should never fail.

James B
I declared it as a local constant and managed to get it to happen again. It still failed on the string assignment.
cduncan
Silly question, but do you have all of the most recent .NET Framework service packs/updates? I don't know that there are any for .NET 4.0 yet, but much of the code still relies on 2.0/3.5 framework classes. Do you have to compile against 4.0? What happens if you target a lower framework, e.g. 3.5?
James B